mirror of
https://github.com/cappuccino/cappuccino.git
synced 2026-11-09 08:28:45 +00:00
Refactor objj_eval to work in the browser. Move from CommonJS-specific code to Objective-J proper.
This commit is contained in:
1 parent
8d02ba7b53
commit
cb6ef75283
3 files changed
+57
-26
No files matched your search
@@ -148,37 +148,12 @@ exports.repl = function()
|
||||
}
|
||||
}
|
||||
|
||||
exports.objj_eval = function(/*String*/ aString)
|
||||
{
|
||||
// We need this while code still refers to window.
|
||||
Executable.setCommonJSArguments(require, exports, module, system, print, window);
|
||||
|
||||
var executable = exports.preprocess(aString, "", 0);
|
||||
|
||||
if (!executable.hasLoadedFileDependencies())
|
||||
executable.loadFileDependencies();
|
||||
|
||||
// A bit of a hack. Executable compiles the code itself into a function, but we want
|
||||
// the raw code to eval here.
|
||||
var code = executable._code;
|
||||
|
||||
// Not clear why these should be global, varing them doesn't seem to take effect with evaluateString.
|
||||
global.objj_executeFile = Executable.fileExecuterForURL(FILE.cwd());
|
||||
global.objj_importFile = Executable.fileImporterForURL(FILE.cwd());
|
||||
|
||||
if (typeof system !== "undefined" && system.engine === "rhino")
|
||||
return Packages.org.mozilla.javascript.Context.getCurrentContext().evaluateString(global, code, "objj_eval", 0, NULL);
|
||||
|
||||
return eval(code);
|
||||
}
|
||||
|
||||
Executable.setCommonJSParameters("require", "exports", "module", "system", "print", "window");
|
||||
|
||||
// creates a narwhal factory function in the objj module scope
|
||||
exports.make_narwhal_factory = function(path)
|
||||
{
|
||||
return function(require, exports, module, system, print)
|
||||
{
|
||||
Executable.setCommonJSParameters("require", "exports", "module", "system", "print", "window");
|
||||
Executable.setCommonJSArguments(require, exports, module, system, print, window);
|
||||
Executable.fileImporterForURL(FILE.dirname(path))(path, YES);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
GLOBAL(objj_eval) = function(/*String*/ aString)
|
||||
{
|
||||
#if COMMONJS
|
||||
var url = FILE.join(FILE.cwd(), "/");
|
||||
Executable.setCommonJSParameters("require", "exports", "module", "system", "print", "window");
|
||||
Executable.setCommonJSArguments(require, exports, module, system, print, window);
|
||||
#else
|
||||
var url = exports.pageURL;
|
||||
#endif
|
||||
|
||||
// Temporarily switch the loader to sychronous mode since objj_eval must be synchronous
|
||||
// therefore you shouldn't use @imports in objj_eval the browser
|
||||
var asyncLoaderSaved = exports.asyncLoader;
|
||||
exports.asyncLoader = NO;
|
||||
|
||||
var executable = exports.preprocess(aString, url, 0);
|
||||
|
||||
if (!executable.hasLoadedFileDependencies())
|
||||
executable.loadFileDependencies();
|
||||
|
||||
// here we setup a scope object containing the free variables that would normally be arguments to the module function
|
||||
global._objj_eval_scope = {};
|
||||
|
||||
global._objj_eval_scope.objj_executeFile = Executable.fileExecuterForURL(url);
|
||||
global._objj_eval_scope.objj_importFile = Executable.fileImporterForURL(url);
|
||||
#if COMMONJS
|
||||
global._objj_eval_scope.require = require;
|
||||
global._objj_eval_scope.exports = exports;
|
||||
global._objj_eval_scope.module = module;
|
||||
global._objj_eval_scope.system = system;
|
||||
global._objj_eval_scope.print = print;
|
||||
global._objj_eval_scope.window = window;
|
||||
#endif
|
||||
|
||||
// A bit of a hack. Executable compiles the code itself into a function, but we want
|
||||
// the raw code to eval here so we can get the result.
|
||||
// No known way to get the result of a statement except via eval.
|
||||
var code = "with(_objj_eval_scope){" + executable._code + "\n//*/\n}";
|
||||
|
||||
var result;
|
||||
#if COMMONJS
|
||||
if (typeof system !== "undefined" && system.engine === "rhino")
|
||||
result = Packages.org.mozilla.javascript.Context.getCurrentContext().evaluateString(global, code, "objj_eval", 0, NULL);
|
||||
else
|
||||
#endif
|
||||
result = eval(code);
|
||||
|
||||
// restore async loader setting
|
||||
exports.asyncLoader = asyncLoaderSaved;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// deprecated, use global
|
||||
exports.objj_eval = objj_eval;
|
||||
@@ -47,6 +47,7 @@
|
||||
#include "Executable.js"
|
||||
#include "FileExecutable.js"
|
||||
#include "Runtime.js"
|
||||
#include "Eval.js"
|
||||
#if defined(DEBUG) || defined(COMMONJS)
|
||||
#include "Debug.js"
|
||||
#endif
|
||||
|
||||
Reference in new issue
Block a user