seanhalle@6: /* seanhalle@6: * File: first.cpp seanhalle@6: * Author: sara seanhalle@6: * seanhalle@6: * Created on December 30, 2013, 11:41 AM seanhalle@6: */ seanhalle@6: seanhalle@6: #include seanhalle@6: /* Include the JSAPI header file to get access to SpiderMonkey. */ seanhalle@6: #include "jsapi.h" seanhalle@6: #include seanhalle@6: seanhalle@6: using namespace std; seanhalle@6: struct thread_parameter seanhalle@6: { seanhalle@6: // char *uncompiledScript; seanhalle@6: // JSObject *sharedGlobalObject; seanhalle@6: // JSRuntime *currRt; seanhalle@6: char *tempstr; seanhalle@6: seanhalle@6: seanhalle@6: }; seanhalle@6: /* The class of the global object. */ seanhalle@6: JSRuntime *rt; seanhalle@6: JSObject *global; seanhalle@6: static JSClass global_class = { seanhalle@6: "global", JSCLASS_GLOBAL_FLAGS, seanhalle@6: JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub, seanhalle@6: JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub, seanhalle@6: JSCLASS_NO_OPTIONAL_MEMBERS seanhalle@6: }; seanhalle@6: seanhalle@6: /* The error reporter callback. */ seanhalle@6: void reportError(JSContext *cx, const char *message, JSErrorReport *report) seanhalle@6: { seanhalle@6: fprintf(stderr, "%s:%u:%s\n", seanhalle@6: report->filename ? report->filename : "", seanhalle@6: (unsigned int) report->lineno, seanhalle@6: message); seanhalle@6: } seanhalle@6: /** the function which the thread executes when is created at main seanhalle@6: *@parameter structure of all the argument the function needs seanhalle@6: *@return return null at the end seanhalle@6: */ seanhalle@6: void *threadFunction(void* arg){ seanhalle@6: /* indicates that the execution of the script is executed successfully or not*/ seanhalle@6: bool execute; seanhalle@6: int temp = 1; seanhalle@6: // char *script = "'Hi' + 'Sara'"; seanhalle@6: char threadScript[] = "'Hi ' + 'Sara!'"; seanhalle@6: char *scr = threadScript; seanhalle@6: JSString *outStr; seanhalle@6: /* carries the value of the last expression of the script*/ seanhalle@6: jsval rval1; seanhalle@6: seanhalle@6: /* The new context which is created by the thread*/ seanhalle@6: JSContext *currContext; seanhalle@6: /* pass the incoming argument of the function */ seanhalle@6: // struct thread_parameter *arg1 = (struct thread_parameter *) arg; seanhalle@6: //const char *uncScript = (const char*)arg->uncompiledScript; seanhalle@6: //scr = (char *)arg; seanhalle@6: /*Creates new context to be attached to the creator thread*/ seanhalle@6: currContext = JS_NewContext(rt, 8192); seanhalle@6: if (currContext == NULL) seanhalle@6: return NULL; seanhalle@6: /* In a thread safe build every entry to the api that uses seanhalle@6: context must be in a request for thread safety*/ seanhalle@6: JS_BeginRequest(currContext); seanhalle@6: JS_SetOptions(currContext, JSOPTION_VAROBJFIX | JSOPTION_JIT | JSOPTION_METHODJIT); seanhalle@6: JS_SetVersion(currContext, JSVERSION_LATEST); seanhalle@6: /*Compiles the incoming script*/ seanhalle@6: JSObject *compiledScript = JS_CompileScript(currContext, global, scr,strlen(scr), "threadFilename", 0); seanhalle@6: /*Executes the compiled script*/ seanhalle@6: execute = JS_ExecuteScript(currContext, global, compiledScript, &rval1); seanhalle@6: if ( rval1 == JS_FALSE)//rval == JS_NULL | seanhalle@6: return NULL; seanhalle@6: /*gets the returned value of the last expression of the executed script*/ seanhalle@6: outStr = JS_ValueToString(currContext, rval1); seanhalle@6: printf("%s\n", JS_EncodeString(currContext, outStr)); seanhalle@6: /*Ends the request */ seanhalle@6: JS_EndRequest(currContext); seanhalle@6: /*Destroy the context of that thread*/ seanhalle@6: JS_DestroyContext(currContext); seanhalle@6: //printf("HI, Sara"); seanhalle@6: return NULL; seanhalle@6: seanhalle@6: } seanhalle@6: /* seanhalle@6: * seanhalle@6: */ seanhalle@6: int main(int argc, const char *argv[]) { seanhalle@6: seanhalle@6: /* JSAPI variables. */ seanhalle@6: //JSRuntime *rt; seanhalle@6: JSContext *cx; seanhalle@6: // JSObject *global; seanhalle@6: /* id of the created thread*/ seanhalle@6: pthread_t thread_id; seanhalle@6: seanhalle@6: /* Create a JS runtime. You always need at least one runtime per process. */ seanhalle@6: rt = JS_NewRuntime(8 * 1024 * 1024); seanhalle@6: if (rt == NULL) seanhalle@6: return 1; seanhalle@6: seanhalle@6: /* seanhalle@6: * Create a context. You always need a context per thread. seanhalle@6: * Note that this program is not multi-threaded. seanhalle@6: This thread is used by the main thread only seanhalle@6: */ seanhalle@6: cx = JS_NewContext(rt, 8192); seanhalle@6: if (cx == NULL) seanhalle@6: return 1; seanhalle@6: JS_BeginRequest(cx); seanhalle@6: JS_SetOptions(cx, JSOPTION_VAROBJFIX | JSOPTION_JIT | JSOPTION_METHODJIT); seanhalle@6: JS_SetVersion(cx, JSVERSION_LATEST); seanhalle@6: JS_SetErrorReporter(cx, reportError); seanhalle@6: seanhalle@6: /* seanhalle@6: * Create the global object in a new compartment. seanhalle@6: * You always need a global object per context. seanhalle@6: */ seanhalle@6: seanhalle@6: global = JS_NewCompartmentAndGlobalObject(cx, &global_class, NULL); seanhalle@6: if (global == NULL) seanhalle@6: return 1; seanhalle@6: seanhalle@6: /* seanhalle@6: * Populate the global object with the standard JavaScript seanhalle@6: * function and object classes, such as Object, Array, Date. seanhalle@6: */ seanhalle@6: if (!JS_InitStandardClasses(cx, global)) seanhalle@6: return 1; seanhalle@6: seanhalle@6: /* Your application code here. This may include JSAPI calls seanhalle@6: * to create your own custom JavaScript objects and to run scripts. seanhalle@6: * seanhalle@6: * The following example code creates a literal JavaScript script, seanhalle@6: * evaluates it, and prints the result to stdout. seanhalle@6: * seanhalle@6: * Errors are conventionally saved in a JSBool variable named ok. seanhalle@6: */ seanhalle@6: seanhalle@6: seanhalle@6: /** Create the thread here....*/ seanhalle@6: struct thread_parameter threadArg; seanhalle@6: //threadArg.sharedGlobalObject = global; seanhalle@6: char threadScript[] = "'Hi ' + 'Sara!'"; seanhalle@6: //threadArg.tempstr = threadScript; seanhalle@6: //threadArg.currRt = rt; seanhalle@6: seanhalle@6: pthread_create(&thread_id, NULL, &threadFunction,NULL); seanhalle@6: /*****************************************************************/ seanhalle@6: const char *script = "'Hello ' + 'World!'"; seanhalle@6: jsval rval; seanhalle@6: JSString *str; seanhalle@6: JSBool ok; seanhalle@6: const char *filename = "noname"; seanhalle@6: uintN lineno = 0; seanhalle@6: ok = JS_EvaluateScript(cx, global, script, strlen(script), seanhalle@6: filename, lineno, &rval); seanhalle@6: if ( rval == JS_FALSE)//rval == JS_NULL | seanhalle@6: return 1; seanhalle@6: str = JS_ValueToString(cx, rval); seanhalle@6: printf("%s\n", JS_EncodeString(cx, str)); seanhalle@6: seanhalle@6: /* End of your application code */ seanhalle@6: seanhalle@6: /* Clean things up and shut down SpiderMonkey. */ seanhalle@6: JS_EndRequest(cx); seanhalle@6: JS_DestroyContext(cx); seanhalle@6: JS_DestroyRuntime(rt); seanhalle@6: JS_ShutDown(); seanhalle@6: seanhalle@6: return 0; seanhalle@6: } seanhalle@6: