Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit a5e38ca7fa263ed8d90c4c05a0ef39566f2ef3a4 @gfx committed Jul 16, 2012
Showing with 56 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +3 −0 Makefile
  3. +50 −0 main.cc
@@ -0,0 +1,3 @@
+*~
+*.swp
+nore
@@ -0,0 +1,3 @@
+
+nore: main.cc
+ $(CXX) -Wall -Wextra -g -framework JavaScriptCore -o $@ $<
50 main.cc
@@ -0,0 +1,50 @@
+#include <JavaScriptCore/JavaScriptCore.h>
+
+#define UNUSED __attribute__((unused))
+
+static JSValueRef jsGlobalPrint(
+ JSContextRef ctx,
+ JSObjectRef jobj UNUSED,
+ JSObjectRef jobjThis UNUSED,
+ size_t argLen,
+ const JSObjectRef args[],
+ JSValueRef* jobjExp) {
+
+ for (size_t i = 0; i < argLen; ++i) {
+ JSStringRef jstrArg = JSValueToStringCopy(ctx, args[0], jobjExp);
+ size_t const maxSize = JSStringGetMaximumUTF8CStringSize(jstrArg);
+ char* const cstr = new char[maxSize];
+ size_t const size = JSStringGetUTF8CString(jstrArg, cstr, maxSize);
+ fwrite(cstr, size, 1, stdout);
+ fwrite("\n", 1, 1, stdout);
+ delete cstr;
+ JSStringRelease(jstrArg);
+ }
+
+ return JSValueMakeUndefined(ctx);
+}
+
+int main(int argc, char** argv) {
+
+ if (argc == 1) {
+ // TODO: interactive mode?
+ exit(0);
+ }
+
+ JSGlobalContextRef ctx = JSGlobalContextCreate(NULL);
+ JSObjectRef jobjGlobal = JSContextGetGlobalObject(ctx);
+
+ JSStringRef jstrPrint = JSStringCreateWithUTF8CString("print");
+ JSObjectRef jfuncPrint = JSObjectMakeFunctionWithCallback(ctx, jstrPrint, (JSObjectCallAsFunctionCallback)jsGlobalPrint);
+ JSObjectSetProperty(ctx, jobjGlobal, jstrPrint, jfuncPrint, kJSPropertyAttributeNone, NULL);
+ JSStringRelease(jstrPrint);
+
+ JSStringRef jstrSource = JSStringCreateWithUTF8CString(argv[1]);
+ JSEvaluateScript(ctx, jstrSource, NULL, NULL, 1, NULL);
+ JSStringRelease(jstrSource);
+
+ JSGlobalContextRelease(ctx);
+ JSGarbageCollect(ctx);
+ return 0;
+}
+

0 comments on commit a5e38ca

Please sign in to comment.