Permalink
Browse files

WIP

  • Loading branch information...
1 parent a5e38ca commit 310c22ce1f93fe7e1c1bfeab5c4baf318efb63e6 @gfx committed Jul 16, 2012
Showing with 119 additions and 52 deletions.
  1. +2 −2 Makefile
  2. +4 −0 hello.js
  3. +113 −0 main.c
  4. +0 −50 main.cc
View
@@ -1,3 +1,3 @@
-nore: main.cc
- $(CXX) -Wall -Wextra -g -framework JavaScriptCore -o $@ $<
+nore: main.c
+ $(CC) -Wall -Wextra -g -framework JavaScriptCore -o $@ $<
View
@@ -0,0 +1,4 @@
+
+print("hello, world!");
+
+console.log("hello, world!");
View
113 main.c
@@ -0,0 +1,113 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+
+#include <JavaScriptCore/JavaScriptCore.h>
+#define UNUSED __attribute__((unused))
+
+static void printJSError(JSContextRef ctx, JSValueRef exception);
+
+static void
+printJSValueRef(JSContextRef ctx, JSValueRef arg, FILE* fp, JSValueRef* exception) {
+ JSStringRef jstr = JSValueToStringCopy(ctx, arg, exception);
+ if (exception && *exception) {
+ printJSError(ctx, *exception);
+ return;
+ }
+ size_t const maxSize = JSStringGetMaximumUTF8CStringSize(jstr);
+ char* const buffer = (char*)malloc(maxSize);
+ size_t const size = JSStringGetUTF8CString(jstr, buffer, maxSize);
+ fwrite(buffer, size, 1, fp);
+ free(buffer);
+ JSStringRelease(jstr);
+}
+
+static void
+printJSError(JSContextRef ctx, JSValueRef exception) {
+ fflush(stdout);
+ printJSValueRef(ctx, exception, stderr, NULL);
+ fputs("\n", stderr);
+}
+
+static JSValueRef
+jsGlobalPrint(
+ JSContextRef ctx,
+ JSObjectRef jobj UNUSED,
+ JSObjectRef jobjThis UNUSED,
+ size_t argLen,
+ const JSValueRef args[],
+ JSValueRef* exception) {
+
+ for (size_t i = 0; i < argLen; ++i) {
+ printJSValueRef(ctx, args[i], stdout, exception);
+ }
+ fputs("\n", stdout);
+
+ return JSValueMakeUndefined(ctx);
+}
+
+int main(int argc, char** argv) {
+
+ if (argc == 1) {
+ // TODO: interactive mode?
+ exit(0);
+ }
+ const char* const fileName = argv[1];
+
+ JSValueRef exception = NULL;
+
+ // initialize globals
+ JSGlobalContextRef ctx = JSGlobalContextCreate(NULL);
+ JSObjectRef jobjGlobal = JSContextGetGlobalObject(ctx);
+
+ // setup node-like environment (console and process)
+ JSStringRef jstrPrint = JSStringCreateWithUTF8CString("print");
+ JSObjectRef jfuncPrint = JSObjectMakeFunctionWithCallback(ctx, jstrPrint, jsGlobalPrint);
+ JSObjectSetProperty(ctx, jobjGlobal, jstrPrint, jfuncPrint, kJSPropertyAttributeNone, NULL);
+ JSStringRelease(jstrPrint);
+
+ JSStringRef sourceContent = NULL;
+ {
+ struct stat st;
+ if (stat(fileName, &st) != 0) {
+ perror("failed to stat");
+ return 1;
+ }
+ size_t const fileSize = (size_t)st.st_size;
+ FILE* fp = fopen(fileName, "r");
+ if (! fp) {
+ perror("failed to open");
+ return 1;
+ }
+ void* const buffer = calloc(fileSize + 1, sizeof(char));
+ assert(buffer);
+ size_t total = 0;
+ size_t nread = 0;
+ while ((nread = fread(buffer + total, fileSize, sizeof(char), fp)) > 0) {
+ total += nread;
+ }
+
+ if (ferror(fp)) {
+ perror("failed to read");
+ return 1;
+ }
+ sourceContent = JSStringCreateWithUTF8CString((const char*)buffer);
+ }
+ JSStringRef sourceFile = JSStringCreateWithUTF8CString(fileName);
+
+ // execute script
+ JSEvaluateScript(ctx, sourceContent, jobjGlobal, sourceFile, 1, &exception);
+ JSStringRelease(sourceContent);
+ JSStringRelease(sourceFile);
+
+ if (exception != NULL) {
+ printJSError(ctx, exception);
+ }
+
+ // finalize globals
+ JSGarbageCollect(ctx);
+ JSGlobalContextRelease(ctx);
+ return 0;
+}
+
View
50 main.cc
@@ -1,50 +0,0 @@
-#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 310c22c

Please sign in to comment.