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