Permalink
Browse files

jscocoa command line interface to JSCocoa

  • Loading branch information...
1 parent 144ff90 commit bcee6d80b305e45f05670bd395971169055ae507 Tom Robinson committed Oct 10, 2008
Showing with 98 additions and 0 deletions.
  1. +31 −0 jscocoa/README
  2. +67 −0 jscocoa/jscocoa.m
  3. 0 {jscore-bridge → jscocoa}/jscore-bridge.m
View
@@ -0,0 +1,31 @@
+"jscocoa" is a simple command line interface to JSCocoa:
+
+- http://inexdo.com/JSCocoa
+- http://code.google.com/p/jscocoa/
+
+1) Create a new command line tool project in Xcode
+2) Replace the main file with jscocoa.m
+
+Perform the steps listed here: http://code.google.com/p/jscocoa/wiki/QuickStart
+
+3) Add JavascriptCore.framework to frameworks
+4) In 'Build' project settings, add -lffi to 'Other linker flags'
+5) Copy (or reference) all the files from the JSCocoa folder in your project.
+
+Only the following are really necessary:
+
+ JSCocoaPrivateObject.m
+ JSCocoaController.h
+ JSCocoaController.m
+ JSCocoaFFIArgument.h
+ JSCocoaFFIArgument.m
+ JSCocoaFFIClosure.h
+ JSCocoaFFIClosure.m
+ JSCocoaHelper.h
+ JSCocoaHelper.m
+ JSCocoaPrivateObject.h
+ BridgeSupportController.m
+ BridgeSupportController.h
+
+
+jscore-bridge.m is very incomplete and obsoleted by JSCocoa.
View
@@ -0,0 +1,67 @@
+#import <Foundation/Foundation.h>
+#import "JSCocoaController.h"
+
+void JSValuePrint(JSContextRef, JSValueRef, JSValueRef *);
+
+int main (int argc, const char * argv[])
+{
+ [[NSAutoreleasePool alloc] init];
+ id c = [JSCocoaController sharedController];
+ JSGlobalContextRef ctx = [c ctx];
+
+ if (argc > 1)
+ {
+ for (int i = 1; i < argc; i++)
+ [c evalJSFile:[NSString stringWithFormat:@"%s", argv[i]]];
+ }
+ else
+ {
+ while (1)
+ {
+ char buffer[1024];
+
+ printf("js> ");
+
+ if (fgets(buffer, 1024, stdin) == NULL)
+ exit(0);
+
+ JSStringRef script = JSStringCreateWithUTF8CString(buffer);
+ JSValueRef exception = NULL;
+
+ if (JSCheckScriptSyntax(ctx, script, 0, 0, &exception) && !exception)
+ {
+ JSValueRef value = JSEvaluateScript(ctx, script, 0, 0, 0, &exception);
+
+ if (exception)
+ JSValuePrint(ctx, exception, NULL);
+
+ if (value && !JSValueIsUndefined(ctx, value))
+ JSValuePrint(ctx, value, &exception);
+ }
+ else
+ {
+ printf("Syntax error\n");
+ }
+
+ JSStringRelease(script);
+ }
+
+ }
+}
+
+void JSValuePrint(
+ JSContextRef ctx,
+ JSValueRef value,
+ JSValueRef *exception)
+{
+ JSStringRef string = JSValueToStringCopy(ctx, value, exception);
+ size_t length = JSStringGetLength(string);
+
+ char *buffer = malloc(length+1);
+ JSStringGetUTF8CString(string, buffer, length+1);
+ JSStringRelease(string);
+
+ puts(buffer);
+
+ free(buffer);
+}
File renamed without changes.

0 comments on commit bcee6d8

Please sign in to comment.