Major improvments to Objective-J Narwhal support.

* Added objj_eval, objj_import_sync, objj_preprocess_sync, etc
* Refactored REPL.
* Added Objective-J plugin for load system, using new Narwhal plugin system.
* Refactored objj and objjc into single module.
This commit is contained in:
Tom Robinson
2009-08-09 14:40:02 -07:00
parent 172c7330ab
commit 2423faa8c6
11 changed files with 143 additions and 242 deletions
+1
View File
@@ -29,6 +29,7 @@ task :build => [:update_submodules, $ENVIRONMENT_DIR] do
cp_r('narwhal', $ENVIRONMENT_NARWHAL_PRODUCT)
cp_r('browserjs', $ENVIRONMENT_BROWSERJS_PRODUCT)
cp_r('jack', $ENVIRONMENT_JACK_PRODUCT)
symlink_executable(File.join($ENVIRONMENT_JACK_PRODUCT, "bin", "jackup"))
end
CLOBBER.include($ENVIRONMENT_NARWHAL_PRODUCT)
+1 -1
View File
@@ -1,3 +1,3 @@
#!/usr/bin/env narwhal
require("objj/objj");
require("objj/objj").run(system.args);
+4 -2
View File
@@ -1,9 +1,11 @@
#!/usr/bin/env narwhal
var objjc = require("objj/objjc"),
var objj = require("objj/objj"),
File = require("file");
with (objjc)
require("objj/regexp-rhino-patch");
with (objj)
{
importPackage(java.lang);
@@ -0,0 +1,23 @@
var objj = null;
function ObjectiveJLoader() {
var loader = {};
var factories = {};
loader.reload = function(topId, path) {
if (!objj) objj = require("objj/objj");
//print("loading objective-j: " + topId + " (" + path + ")");
factories[topId] = objj.make_narwhal_factory(system.fs.read(path)/*, topId, path*/);
}
loader.load = function(topId, path) {
if (!factories.hasOwnProperty(topId))
loader.reload(topId, path);
return factories[topId];
}
return loader;
};
require.loader.loaders.unshift([".j", ObjectiveJLoader()]);
+111 -46
View File
@@ -1,33 +1,60 @@
var File = require("file");
var window = require("browser/window");
var OBJJ_HOME = File.resolve(module.path, "..", ".."),
FRAMEWORKS = File.resolve(OBJJ_HOME, "lib/", "Frameworks/"),
OBJECTIVEJ = File.resolve(FRAMEWORKS, "Objective-J/", "rhino.platform/", "Objective-J.js");
// variables to be exported from the module, for use in objjc, etc
var exported = [
"objj_preprocess",
"FRAGMENT_FILE", "FRAGMENT_LOCAL",
"MARKER_CODE", "MARKER_IMPORT_STD", "MARKER_IMPORT_LOCAL",
"OBJJ_PREPROCESSOR_DEBUG_SYMBOLS"
];
window.OBJJ_INCLUDE_PATHS = [FRAMEWORKS];
// setup OBJJ_HOME, OBJJ_INCLUDE_PATHS, etc
var OBJJ_HOME = exports.OBJJ_HOME = File.resolve(module.path, "..", ".."),
frameworksPath = File.resolve(OBJJ_HOME, "lib/", "Frameworks/"),
objectivejPath = File.resolve(frameworksPath, "Objective-J/", "rhino.platform/", "Objective-J.js");
window.OBJJ_INCLUDE_PATHS = [frameworksPath];
if (system.env["OBJJ_INCLUDE_PATHS"])
window.OBJJ_INCLUDE_PATHS = system.env["OBJJ_INCLUDE_PATHS"].split(":").concat(window.OBJJ_INCLUDE_PATHS);
//if (system.args.length > 0)
// window.OBJJ_MAIN_FILE = File.canonical(args.shift());
window.args = system.args;
// FIXME: ARGS
system.args.shift();
// bring the "window" object into scope.
// TODO: somehow make window object the top scope?
with (window)
{
eval(File.read(OBJECTIVEJ, { charset:"UTF-8" }));
// read and eval Objective-J.js with the module's scope
eval(File.read(objectivejPath, { charset:"UTF-8" }));
// export desired variables. must eval variable name to obtain a reference
for (var i = 0; i < exported.length; i++)
exports[exported[i]] = eval(exported[i]);
if (system.args.length > 0)
/*
objj_set_evaluator(function(code) {
return function(OBJJ_CURRENT_BUNDLE) {
with (window) {
return eval("function(OBJJ_CURRENT_BUNDLE){"+code+"}");
}
}
});
*/
// runs the objj repl or file provided in args
exports.run = function(args)
{
args = args || [];
window.args = args;
// FIXME: ARGS
args.shift();
if (args.length > 0)
{
while (system.args.length && system.args[0].indexOf('-I') === 0)
OBJJ_INCLUDE_PATHS = system.args.shift().substr(2).split(':').concat(OBJJ_INCLUDE_PATHS);
while (args.length && args[0].indexOf('-I') === 0)
OBJJ_INCLUDE_PATHS = args.shift().substr(2).split(':').concat(OBJJ_INCLUDE_PATHS);
var mainFilePath = File.canonical(args.shift());
objj_import(mainFilePath, YES, function() {
if (typeof main === "function")
main.apply(main, args);
@@ -41,38 +68,76 @@ with (window)
system.stdout.write("objj> ").flush();
var input = system.stdin.readLine(),
fragments = objj_preprocess(input, new objj_bundle(), new objj_file(), OBJJ_PREPROCESSOR_DEBUG_SYMBOLS),
count = fragments.length,
ctx = (new objj_context);
if (count == 1 && (fragments[0].type & FRAGMENT_CODE))
{
var fragment = fragments[0];
var result = eval(fragment.info);
if (result != undefined)
print(result);
}
else if (count > 0)
{
while (count--)
{
var fragment = fragments[count];
if (fragment.type & FRAGMENT_FILE)
objj_request_file(fragment.info, (fragment.type & FRAGMENT_LOCAL), NULL);
ctx.pushFragment(fragment);
}
ctx.schedule();
}
require("browser/timeout").serviceTimeouts();
result = objj_eval(input);
if (result !== undefined)
print(result);
} catch (e) {
print(e);
}
require("browser/timeout").serviceTimeouts();
}
}
require("browser/timeout").serviceTimeouts();
}
// synchronously evals Objective-J code
var objj_eval = exports.objj_eval = function(code)
{
var result = eval(objj_preprocess_sync(code));
//require("browser/timeout").serviceTimeouts();
return result;
}
// prepocesses Objective-J code into JavaScript, which will perform imports synchronously when eval'd
var objj_preprocess_sync = function(code)
{
var fragments = objj_preprocess(code, new objj_bundle(), new objj_file(), OBJJ_PREPROCESSOR_DEBUG_SYMBOLS)
var preprocessed = [];
fragments.forEach(function(fragment) {
if (fragment.type & FRAGMENT_CODE)
preprocessed.push(fragment.info);
else if (fragment.type & FRAGMENT_LOCAL)
preprocessed.push("objj_import_sync('"+fragment.info+"',YES);");
else
preprocessed.push("objj_import_sync('"+fragment.info+"',NO);");
});
return preprocessed.join("\n");
}
// synchronously perform an import
var objj_import_sync = function(pathOrPaths, isLocal)
{
var context = new objj_context();
context.pushFragment(fragment_create_file(pathOrPaths, new objj_bundle(), isLocal, NULL));
context.evaluate();
// HACK: need a real synchronous require
// FIXME: this is bad. not really synchronous. shouldn't have to call serviceTimeouts.
require("browser/timeout").serviceTimeouts();
}
// creates a narwhal factory function in the objj module scope
exports.make_narwhal_factory = function(code) {
// TODO: integrate better with objj load system so relative paths work
var OBJJ_CURRENT_BUNDLE = new objj_bundle();
return eval(
"(function(require,exports,module,system,print){" +
objj_preprocess_sync(code) +
"/**/\n})"
);
}
} // end "with"
if (require.main == module.id)
exports.run(system.args);
@@ -1,21 +0,0 @@
var File = require("file");
var window = require("browser/window");
require("./regexp-rhino-patch");
var exported = ["OBJJ_HOME", "objj_preprocess",
"FRAGMENT_FILE", "FRAGMENT_LOCAL",
"MARKER_CODE", "MARKER_IMPORT_STD", "MARKER_IMPORT_LOCAL",
"OBJJ_PREPROCESSOR_DEBUG_SYMBOLS"];
var OBJJ_HOME = File.resolve(module.path, "..", ".."),
FRAMEWORKS = File.resolve(OBJJ_HOME, "lib/", "Frameworks/"),
OBJECTIVEJ = File.resolve(FRAMEWORKS, "Objective-J/", "rhino.platform/", "Objective-J.js");
with (window)
{
eval(File.read(OBJECTIVEJ, { charset:"UTF-8" }).toString());
for (var i = 0; i < exported.length; i++)
exports[exported[i]] = eval(exported[i]);
}
-78
View File
@@ -1,78 +0,0 @@
var File = require("file");
var window = require("browser/window");
var OBJJ_HOME = system.prefix + "/..";
window.OBJJ_INCLUDE_PATHS = [OBJJ_HOME+"/lib/Frameworks/"];
if (system.env["OBJJ_INCLUDE_PATHS"])
window.OBJJ_INCLUDE_PATHS = system.env["OBJJ_INCLUDE_PATHS"].split(":").concat(window.OBJJ_INCLUDE_PATHS);
//if (system.args.length > 0)
// window.OBJJ_MAIN_FILE = String((new Packages.java.io.File(args.shift())).getAbsolutePath());
window.args = system.args;
// FIXME: ARGS
system.args.shift();
with (window)
{
eval(File.read(OBJJ_HOME + "/lib/Frameworks/Objective-J/rhino.platform/Objective-J.js", { charset:"UTF-8" }));
if (system.args.length > 0)
{
while (system.args.length && system.args[0].indexOf('-I') === 0)
OBJJ_INCLUDE_PATHS = system.args.shift().substr(2).split(':').concat(OBJJ_INCLUDE_PATHS);
var mainFilePath = String((new Packages.java.io.File(args.shift())).getAbsolutePath());
objj_import(mainFilePath, YES, function() {
if (typeof main === "function")
main.apply(main, args);
});
}
else
{
var br = new Packages.java.io.BufferedReader(new Packages.java.io.InputStreamReader(Packages.java.lang.System["in"], "UTF-8"));
while (true)
{
try {
Packages.java.lang.System.out.print("objj> ");
var input = String(br.readLine()),
fragments = objj_preprocess(input, new objj_bundle(), new objj_file(), OBJJ_PREPROCESSOR_DEBUG_SYMBOLS),
count = fragments.length,
ctx = (new objj_context);
if (count == 1 && (fragments[0].type & FRAGMENT_CODE))
{
var fragment = fragments[0];
var result = eval(fragment.info);
if (result != undefined)
print(result);
}
else if (count > 0)
{
while (count--)
{
var fragment = fragments[count];
if (fragment.type & FRAGMENT_FILE)
objj_request_file(fragment.info, (fragment.type & FRAGMENT_LOCAL), NULL);
ctx.pushFragment(fragment);
}
ctx.schedule();
}
require("browser/timeout").serviceTimeouts();
} catch (e) {
print(e);
}
}
}
require("browser/timeout").serviceTimeouts();
}
-19
View File
@@ -1,19 +0,0 @@
var File = require("file");
var window = require("browser/window");
require("./regexp-rhino-patch");
var exported = ["OBJJ_HOME", "objj_preprocess",
"FRAGMENT_FILE", "FRAGMENT_LOCAL",
"MARKER_CODE", "MARKER_IMPORT_STD", "MARKER_IMPORT_LOCAL",
"OBJJ_PREPROCESSOR_DEBUG_SYMBOLS"];
var OBJJ_HOME = system.prefix + "/..";
with (window)
{
eval(File.read(OBJJ_HOME + "/lib/Frameworks/Objective-J/rhino.platform/Objective-J.js", { charset:"UTF-8" }).toString());
for (var i = 0; i < exported.length; i++)
exports[exported[i]] = eval(exported[i]);
}
@@ -1,73 +0,0 @@
var CachedRegexData = [];
function regexDataFromRegex(aRegex)
{
var source = "",
flags = "";
if (typeof aRegex == "string")
{
if (CachedRegexData[aRegex])
return CachedRegexData[aRegex];
string = aRegex;
source = aRegex;
}
else
{
string = aRegex.toString();
if (CachedRegexData[string])
return CachedRegexData[string];
var index = string.lastIndexOf('/');
source = string.substr(0, index);
flags = string.substr(index + 1);
}
source = source.replace("\\[\\^\\\\\\d\\]", ".", "g");
source = source.replace("\\[([^\\]]*)\\\\b([^\\]]*)\\]", "[$1\\\\cH$2]", "g") // [...\b...] -> [...\cH...]
source = source.replace("(?<!\\\\)\\{(?!\\d)", "\\\\{", "g");
source = source.replace("(?<!(\\d,?|\\\\))\\}", "\\\\}", "g");
return CachedRegexData[string] = { pattern:java.util.regex.Pattern.compile(source, 0), flags:flags };
}
function newMatch(regex)
{
var regexData = regexDataFromRegex(regex);
var matcher = regexData.pattern.matcher(new java.lang.String(this)),
flags = regexData.flags;
if (!matcher.find())
return [];
var index = matcher.start(0),
groups = [];
if (flags.indexOf('g') != -1)
{
do
{
groups.push(matcher.group(0) + "");
}
while (matcher.find());
}
else
{
for (index = 0; index <= matcher.groupCount(); ++index)
{
var group = matcher.group(index);
if (group != null)
groups[index] = group;
}
}
return groups;
}
if (system.platform === "rhino")
String.prototype.match = newMatch;
+2 -1
View File
@@ -2,5 +2,6 @@
"author": "Tom Robinson",
"dependencies": [],
"lib": "lib-js",
"jars": "jars/"
"jars": "jars/",
"plugins": ["objj/loader"]
}