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