diff spidermonkey-playground.cpp @ 1:7444443d2646

added simple script and wrapper.cpp from jetpack
author Atul Varma <varmaa@toolness.com>
date Thu, 18 Jun 2009 20:35:22 -0700
parents spidermonkey-playground.c@d14fb1d5326c
children 1f3e9c8df4f0
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/spidermonkey-playground.cpp	Thu Jun 18 20:35:22 2009 -0700
@@ -0,0 +1,111 @@
+#include "string.h"
+#include "jsapi.h"
+
+#include "wrapper.h"
+
+/* The class of the global object. */
+static JSClass global_class = {
+  "global", JSCLASS_GLOBAL_FLAGS,
+  JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
+  JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
+  JSCLASS_NO_OPTIONAL_MEMBERS
+};
+
+/* The error reporter callback. */
+static void reportError(JSContext *cx, const char *message,
+                        JSErrorReport *report)
+{
+  fprintf(stderr, "%s:%u:%s\n",
+          report->filename ? report->filename : "<no filename>",
+          (unsigned int) report->lineno,
+          message);
+}
+
+static JSBool print(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
+                    jsval *rval)
+{
+  char *str;
+
+  if (!JS_ConvertArguments(cx, argc, argv, "s", &str))
+    return JS_FALSE;
+
+  printf("%s\n", str);
+
+  return JS_TRUE;
+}
+
+static JSBool wrap(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
+                   jsval *rval)
+{
+  JSObject *wrappee;
+  JSObject *resolver;
+
+  if (!JS_ConvertArguments(cx, argc, argv, "oo", &wrappee, &resolver))
+    return JS_FALSE;
+
+  JSObject *result;
+
+  result = wrapObject(cx, argv[0], argv[1]);
+
+  *rval = OBJECT_TO_JSVAL(result);
+  return JS_TRUE;
+}
+
+static JSFunctionSpec global_functions[] = {
+  JS_FS("print",   print,   1, 0, 0),
+  JS_FS("wrap",    wrap,    2, 0, 0),
+  JS_FS_END
+};
+
+int main(int argc, const char *argv[])
+{
+  /* JS variables. */
+  JSRuntime *rt;
+  JSContext *cx;
+  JSObject  *global;
+
+  /* Create a JS runtime. */
+  rt = JS_NewRuntime(8L * 1024L * 1024L);
+  if (rt == NULL)
+    return 1;
+
+  /* Create a context. */
+  cx = JS_NewContext(rt, 8192);
+  if (cx == NULL)
+    return 1;
+  JS_SetOptions(cx, JSOPTION_VAROBJFIX);
+  JS_SetVersion(cx, JSVERSION_LATEST);
+  JS_SetErrorReporter(cx, reportError);
+
+  /* Create the global object. */
+  global = JS_NewObject(cx, &global_class, NULL, NULL);
+  if (global == NULL)
+    return 1;
+
+  /* Populate the global object with the standard globals,
+     like Object and Array. */
+  if (!JS_InitStandardClasses(cx, global))
+    return 1;
+
+  if (!JS_DefineFunctions(cx, global, global_functions))
+    return 1;
+
+  /* Your application code here. This may include JSAPI calls
+     to create your own custom JS objects and run scripts. */
+  char *source = "print('Hello World.'); wrap({}, {});";
+  jsval rval;
+  if (!JS_EvaluateScript(cx, global, source,
+                         strlen(source), "<string>", 1,
+                         &rval)) {
+    printf("An error occurred.\n");
+    return 1;
+  }
+
+  /* Cleanup. */
+  JS_DestroyContext(cx);
+  JS_DestroyRuntime(rt);
+  JS_ShutDown();
+
+  printf("Farewell.\n");
+  return 0;
+}