Permalink
Browse files

Refactor for dynamically linked editline support.

  • Loading branch information...
1 parent 854f5f7 commit 21352c61cdd62b61af11e1c64b6adec861d19c2d Tom Robinson committed Jun 4, 2010
Showing with 85 additions and 98 deletions.
  1. +16 −5 Makefile
  2. +58 −39 bootstrap.js
  3. +3 −49 narwhal.c
  4. +8 −5 src/readline.cc
View
21 Makefile
@@ -11,11 +11,9 @@ FRAMEWORKS_DIR=frameworks
LIB_DIR =lib
-READLINE_FLAGS=-DUSE_READLINE -lreadline
-
INCLUDES =-Iinclude
MODULES =$(patsubst %.cc,%.dylib,$(patsubst src/%,lib/%,$(shell find src -name '*.cc')))
-LIBS =-framework JavaScriptCore -L/usr/lib -liconv -L$(LIB_DIR) -lnarwhal $(FRAMEWORKS) $(READLINE_FLAGS)
+LIBS =-framework JavaScriptCore -L/usr/lib -liconv -L$(LIB_DIR) -lnarwhal $(FRAMEWORKS)
SOURCE =narwhal-jsc.c
@@ -46,7 +44,7 @@ jscocoa: config frameworks-jscocoa bin/narwhal-jscocoa modules config-jscocoa
lib/libnarwhal.dylib: narwhal.c
- $(CC) -o $@ $< -dynamiclib $(CPPFLAGS) $(INCLUDES) $(FRAMEWORKS) -framework JavaScriptCore $(READLINE_FLAGS)
+ $(CC) -o $@ $< -dynamiclib $(CPPFLAGS) $(INCLUDES) $(FRAMEWORKS) -framework JavaScriptCore
bin/narwhal-jscore: $(SOURCE) lib/libnarwhal.dylib
mkdir -p `dirname $@`
@@ -99,12 +97,18 @@ modules: $(MODULES) rewrite-lib-paths
rewrite-lib-paths:
#find lib -name "*.dylib" \! -path "*.dSYM*" -exec install_name_tool -change "$(SYSTEM_JSC)" "$(RELATIVE_JSC)" {} \;
find lib -name "*.dylib" \! -path "*.dSYM*" -exec install_name_tool -change "$(ABSOLUTE_LIBNARWHAL)" "$(RELATIVE_LIBNARWHAL)" {} \;
-
+
lib/%.dylib: src/%.cc
mkdir -p `dirname $@`
$(CPP) -o $@ $< $(CPPFLAGS) $(INCLUDES) -dynamiclib $(LIBS)
#install_name_tool -change "$(SYSTEM_JSC)" "$(RELATIVE_JSC)" "$@"
+lib/readline.dylib: src/readline.cc lib/libedit.dylib
+ mkdir -p `dirname $@`
+ $(CPP) -o $@ $< $(CPPFLAGS) $(INCLUDES) -dynamiclib $(LIBS) -DUSE_EDITLINE -Ideps/libedit-20100424-3.0/src -ledit
+ #install_name_tool -change "$(SYSTEM_JSC)" "$(RELATIVE_JSC)" "$@"
+ install_name_tool -change "lib/libedit.dylib" "@executable_path/../lib/libedit.dylib" "$@"
+
lib/jack/handler/jill.dylib: src/jack/handler/jill.cc deps/http-parser/http_parser.o lib/io-engine.dylib lib/binary-engine.dylib
mkdir -p `dirname $@`
$(CPP) -o $@ $< $(CPPFLAGS) $(INCLUDES) -dynamiclib $(LIBS) deps/http-parser/http_parser.o lib/io-engine.dylib lib/binary-engine.dylib
@@ -115,7 +119,14 @@ lib/jack/handler/jill.dylib: src/jack/handler/jill.cc deps/http-parser/http_pars
deps/http-parser/http_parser.o:
cd deps/http-parser && make http_parser.o
+deps/libedit-20100424-3.0/src/.libs/libedit.dylib: deps/libedit-20100424-3.0
+ cd deps/libedit-20100424-3.0 && \
+ env CFLAGS="-force_cpusubtype_ALL -mmacosx-version-min=10.4 -arch i386 -arch ppc" \
+ LDFLAGS="-force_cpusubtype_ALL -mmacosx-version-min=10.4 -arch i386 -arch ppc" ./configure --disable-dependency-tracking && \
+ make
+lib/libedit.dylib: deps/libedit-20100424-3.0/src/.libs/libedit.dylib
+ cp $< $@
frameworks: $(JSCORE_FRAMEWORK)
View
97 bootstrap.js
@@ -1,82 +1,101 @@
(function bootstrap(evalGlobal, global) {
+ var env = ENV;
+ var args = ARGS;
+
function envEnabled(name) {
- return ENV[name] && parseInt(ENV[name]) !== 0;
+ return env[name] && parseInt(env[name]) !== 0;
}
-
+
var profiling = false;
if (typeof _inspector !== "undefined" && envEnabled("NARWHAL_PROFILE")) {
profiling = true;
_inspector.startProfilingJavaScript_();
}
-
+
if (typeof _inspector !== "undefined" && envEnabled("NARWHAL_DEBUGGER")) {
_inspector.show_();
_inspector.startDebuggingJavaScript_();
}
- var prefix = ENV['NARWHAL_HOME'];
- var enginePrefix = ENV['NARWHAL_ENGINE_HOME'];
+ var runShell = envEnabled("NARWHAL_RUN_SHELL") && args.length <= 1;
+
+ var prefix = env['NARWHAL_HOME'];
+ var enginePrefix = env['NARWHAL_ENGINE_HOME'];
var debug = envEnabled("NARWHAL_DEBUG");
var verbose = envEnabled("NARWHAL_DEBUG");
var _isFile = isFile, _read = read, _print = print;
delete read, isFile, print;
-
+
function NativeLoader() {
var loader = {};
var factories = {};
-
+
loader.reload = function reload(topId, path) {
factories[topId] = requireNative(topId, path);
}
-
+
loader.load = function load(topId, path) {
if (!factories.hasOwnProperty(topId))
loader.reload(topId, path);
return factories[topId];
}
-
+
return loader;
};
var sourceURLTag = "\n//@ sourceURL="+prefix+"/narwhal.js";
var narwhal = evalGlobal(_read(prefix + "/narwhal.js") + "/**/" + sourceURLTag);
narwhal.displayName = "narwhal";
-
-try {
- narwhal({
- global: global,
- evalGlobal: evalGlobal,
- engine: 'jsc',
- engines: ['jsc', 'c', 'default'],
- prefix: prefix,
- prefixes: [enginePrefix, prefix],
- print: function print(string) { _print(String(string)); },
- evaluate: function evaluate(text, fileName, lineNumber) {
- var sourceURLTag = "\n//@ sourceURL=" + fileName;
- //return new Function("require", "exports", "module", "system", "print", text+"/**/"+sourceURLTag);
- return eval("(function(require,exports,module,system,print){" + text + "/**/\n})"+sourceURLTag);
- },
- fs: {
- read: function read(path) { return _read(path); },
- isFile: function isFile(path) { return _isFile(path); }
- },
- loaders: [[".dylib", NativeLoader()]],
- os : "darwin",
- debug: debug,
- verbose: verbose
- });
-} catch (e) {
- if (e && (e.line || e.sourceURL))
- print("Error on line " + (e.line || "[unknown]") + " of file " + (e.sourceURL || "[unknown]"));
- throw e;
-}
-
+
+ try {
+ narwhal({
+ global: global,
+ evalGlobal: evalGlobal,
+ engine: 'jsc',
+ engines: ['jsc', 'c', 'default'],
+ prefix: prefix,
+ prefixes: [enginePrefix, prefix],
+ print: function print(string) { _print(String(string)); },
+ evaluate: function evaluate(text, fileName, lineNumber) {
+ var sourceURLTag = "\n//@ sourceURL=" + fileName;
+ //return new Function("require", "exports", "module", "system", "print", text+"/**/"+sourceURLTag);
+ return eval("(function(require,exports,module,system,print){" + text + "/**/\n})"+sourceURLTag);
+ },
+ fs: {
+ read: function read(path) { return _read(path); },
+ isFile: function isFile(path) { return _isFile(path); }
+ },
+ loaders: [[".dylib", NativeLoader()]],
+ os : "darwin",
+ debug: debug,
+ verbose: verbose
+ });
+
+ } catch (e) {
+ if (e && (e.line || e.sourceURL))
+ print("Error on line " + (e.line || "[unknown]") + " of file " + (e.sourceURL || "[unknown]"));
+ throw e;
+ }
+
if (profiling) {
_inspector.stopProfilingJavaScript_();
}
+ if (runShell) {
+ print("Narwhal version "+env["NARWHAL_VERSION"]+", JavaScriptCore engine");
+ while (true) {
+ try {
+ var line = require("readline").readline("> ");
+ var result = evalGlobal(line);
+ print(result);
+ } catch (e) {
+ print(e);
+ }
+ }
+ }
+
})(function evalGlobal() {
return eval(arguments[0]);
}, this);
View
52 narwhal.c
@@ -2,12 +2,6 @@
#include <narwhal.h>
-#ifdef USE_READLINE
-// #include <editline/readline.h>
-#include <readline/history.h>
-#include <readline/readline.h>
-#endif
-
//#ifdef WEBKIT
JSObjectRef JSObjectMakeArray(JSContextRef _context, size_t argc, const JSValueRef argv[], JSValueRef* _exception)
@@ -191,45 +185,6 @@ void* EvaluateREPL(JSContextRef _context, JSStringRef source)
}
}
-// The read-eval-execute loop of the shell.
-void* RunREPL(JSContextRef _context) {
- printf("Narwhal version %s, JavaScriptCore engine\n", NARWHAL_VERSION);
- while (true)
- {
-#ifdef JSCOCOA
- NSAutoreleasePool *pool = [NSAutoreleasePool new];
-#endif
-#ifdef USE_READLINE
- char *str = readline("> ");
- if (str && *str)
- add_history(str);
-
- if (!str)
- break;
-
- JSStringRef source = JSStringCreateWithUTF8CString(str);
- free(str);
-#else
- char str[1024*10];
-
- printf("> ");
- if (!fgets(str, sizeof(str), stdin))
- break;
-
- JSStringRef source = JSStringCreateWithUTF8CString(str);
-#endif
-
- EvaluateREPL(_context, source);
-
- JSStringRelease(source);
-
-#ifdef JSCOCOA
- [pool drain];
-#endif
- }
- printf("\n");
-}
-
DESTRUCTOR(Context_finalize)
{
GET_INTERNAL(ContextPrivate *, data, object);
@@ -386,16 +341,15 @@ JSValueRef narwhal_wrapped(JSGlobalContextRef _context, JSValueRef *_exception,
SET_VALUE(ENV, "NARWHAL_HOME", JS_str_utf8(NARWHAL_HOME, strlen(NARWHAL_HOME)));
SET_VALUE(ENV, "NARWHAL_ENGINE_HOME", JS_str_utf8(NARWHAL_ENGINE_HOME, strlen(NARWHAL_ENGINE_HOME)));
+ SET_VALUE(ENV, "NARWHAL_RUN_SHELL", runShell ? JS_str_utf8("1", 1) : JS_str_utf8("0", 1));
+ SET_VALUE(ENV, "NARWHAL_VERSION", JS_str_utf8(NARWHAL_VERSION, strlen(NARWHAL_VERSION)));
+
JSObjectRef context = CALL(Context_new, _context);
ARGS_ARRAY(init_args, context, ARGS, ENV);
// HACK: call directly
NW_inititialize(_context, NULL, NULL, 3, init_args, _exception);
//CALL_AS_FUNCTION(JS_fn(NW_inititialize), JS_GLOBAL, 3, init_args);
-
- // TODO: move this to JS.
- if (!*_exception && argc <= 1 && runShell)
- RunREPL(_context);
}
int narwhal(JSGlobalContextRef _context, JSValueRef *_exception, int argc, char *argv[], char *envp[], int runShell)
View
13 src/readline.cc
@@ -1,14 +1,17 @@
#include <narwhal.h>
#ifdef USE_READLINE
-// #include <editline/readline.h>
#include <readline/history.h>
#include <readline/readline.h>
#endif
+#ifdef USE_EDITLINE
+#include <editline/readline.h>
+#endif
+
FUNCTION(READLINE_readline)
{
-#ifdef USE_READLINE
+#if defined(USE_READLINE) || defined(USE_EDITLINE)
char *str;
if (ARGC == 0) {
str = readline(NULL);
@@ -26,18 +29,18 @@ FUNCTION(READLINE_readline)
return JS_str_utf8(str, strlen(str));
#else
- THROW("Not compiled with readline support.")
+ THROW("Not compiled with readline or editline support.")
#endif
}
END
FUNCTION(READLINE_addHistory, ARG_UTF8(str))
{
-#ifdef USE_READLINE
+#if defined(USE_READLINE) || defined(USE_EDITLINE)
add_history(str);
return JS_null;
#else
- THROW("Not compiled with readline support.")
+ THROW("Not compiled with readline or editline support.")
#endif
}
END

0 comments on commit 21352c6

Please sign in to comment.