diff --git a/sandboxedModule/ru/application.js b/sandboxedModule/ru/application.js index b66eff8..b1c4d79 100644 --- a/sandboxedModule/ru/application.js +++ b/sandboxedModule/ru/application.js @@ -2,10 +2,42 @@ // прикладного приложения, загружаемого в песочницу демонстрационным // кусочком фреймворка. Читайте README.md в нем задания. +util = require ('util'); + // Вывод из глобального контекста модуля console.log('From application global context'); +var Country = function (name, capital) { + this.name = name; + this.capital = capital; + } + module.exports = function() { // Вывод из контекста экспортируемой функции console.log('From application exported function'); + + //Задание 9 + console.log(inspect(global, {colors: true})); +}; + +//Задания 7-8 +module.exports.func = function(argument, anotherArgum) { + console.log(argument); }; + +module.exports.variable = 100; + +//Задание 1 +setTimeout(() => { + console.log("setTimeout function"); + }, 3000); + +setInterval(() => { + console.log("setInterval function"); +}, 1000); + +//Задание 2 +var newCountry = new Country("Ukraine", "Kyiv"); +console.log(util.format("Call util.inspect() through util.format(): %s", + util.inspect(newCountry)) +); \ No newline at end of file diff --git a/sandboxedModule/ru/framework.js b/sandboxedModule/ru/framework.js index 03a0e99..d600ef2 100644 --- a/sandboxedModule/ru/framework.js +++ b/sandboxedModule/ru/framework.js @@ -6,21 +6,86 @@ // Фреймворк может явно зависеть от библиотек через dependency lookup var fs = require('fs'), vm = require('vm'); + util = require ('util'); + +// Задания 4-5 +var newConsole = {}; +newConsole.log = function(message) { + fs.appendFile('file.txt', fileName + " " + new Date() + " " + message + '\n', + (err) => { + if (err) + throw err; + }); + console.log(fileName + " " + new Date() + " " + message); +} + +//Задание 6 +var newRequire = function(module) { + fs.appendFile('file.txt', new Date() + " " + module + '\n', + (err) => { + if (err) + throw err; + }); + return require(module); +} // Создаем контекст-песочницу, которая станет глобальным контекстом приложения -var context = { module: {}, console: console }; +var context = { module: {}, console: newConsole, setTimeout: setTimeout, + setInterval: setInterval, util: util, require: newRequire, + inspect: util.inspect }; context.global = context; var sandbox = vm.createContext(context); +var keys = {}; +for (var tmp in sandbox) + keys[tmp] = sandbox[tmp]; + // Читаем исходный код приложения из файла -var fileName = './application.js'; +// Задание 3 +var fileName = process.argv[2] == null?'./application.js':process.argv[2]; fs.readFile(fileName, function(err, src) { // Тут нужно обработать ошибки - + if (err) + throw err; + // Запускаем код приложения в песочнице var script = vm.createScript(src, fileName); script.runInNewContext(sandbox); + //Задание 10 + console.log("----------TASK 10----------"); + var newKeys = {}; + for (var tmp in sandbox) + newKeys[tmp] = sandbox[tmp]; + + console.log("New keys:"); + for (var tmp in newKeys) { + if (!(tmp in keys)) + console.log(tmp); + } + + console.log("Deleted keys:"); + for (var tmp in keys) { + if (!(tmp in newKeys)) + console.log(tmp); + } + + //Задание 7 + console.log("----------TASK 7----------"); + for (var tmp in sandbox.module.exports) { + console.log(tmp + " -> " + typeof sandbox.module.exports[tmp]); + } + + //Задание 8 + console.log("----------TASK 8----------"); + console.log(sandbox.module.exports.func.toString()); + console.log('Amount of arguments: '+ + sandbox.module.exports.func.toString(). + replace(/.+\(/, '').replace(/\)[^]+/, ''). + split(/, */).length); + + sandbox.module.exports(); // Забираем ссылку из sandbox.module.exports, можем ее исполнить, // сохранить в кеш, вывести на экран исходный код приложения и т.д. + });