Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
gfx committed Jul 16, 2012
0 parents commit a5e38ca
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,3 @@
*~
*.swp
nore
3 changes: 3 additions & 0 deletions Makefile
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,3 @@

nore: main.cc
$(CXX) -Wall -Wextra -g -framework JavaScriptCore -o $@ $<
50 changes: 50 additions & 0 deletions main.cc
Original file line number Original file line Diff line number Diff line change
@@ -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.