Mercurial > cgi-bin > hgwebdir.cgi > PR > PR_Integrations > javascript > SpiderMonkey
comparison CppApplication_1 (copy)/first.cpp @ 2:993424504eb7
This netbeans project for single thread application
it didn't work yet, may be the spidermonkey's version
is the reason and I'll try to solve this
| author | Sara |
|---|---|
| date | Sun, 05 Jan 2014 14:13:51 -0800 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:c947207cc966 |
|---|---|
| 1 /* | |
| 2 * File: first.cpp | |
| 3 * Author: sara | |
| 4 * | |
| 5 * Created on December 30, 2013, 11:41 AM | |
| 6 */ | |
| 7 | |
| 8 #include <cstdlib> | |
| 9 /* Include the JSAPI header file to get access to SpiderMonkey. */ | |
| 10 #include "jsapi.h" | |
| 11 #include <pthread.h> | |
| 12 | |
| 13 using namespace std; | |
| 14 struct thread_parameter | |
| 15 { | |
| 16 // char *uncompiledScript; | |
| 17 // JSObject *sharedGlobalObject; | |
| 18 // JSRuntime *currRt; | |
| 19 char *tempstr; | |
| 20 | |
| 21 | |
| 22 }; | |
| 23 /* The class of the global object. */ | |
| 24 JSRuntime *rt; | |
| 25 JSObject *global; | |
| 26 static JSClass global_class = { | |
| 27 "global", JSCLASS_GLOBAL_FLAGS, | |
| 28 JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub, | |
| 29 JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub, | |
| 30 JSCLASS_NO_OPTIONAL_MEMBERS | |
| 31 }; | |
| 32 | |
| 33 /* The error reporter callback. */ | |
| 34 void reportError(JSContext *cx, const char *message, JSErrorReport *report) | |
| 35 { | |
| 36 fprintf(stderr, "%s:%u:%s\n", | |
| 37 report->filename ? report->filename : "<no filename=\"filename\">", | |
| 38 (unsigned int) report->lineno, | |
| 39 message); | |
| 40 } | |
| 41 /** the function which the thread executes when is created at main | |
| 42 *@parameter structure of all the argument the function needs | |
| 43 *@return return null at the end | |
| 44 */ | |
| 45 void *threadFunction(void* arg){ | |
| 46 /* indicates that the execution of the script is executed successfully or not*/ | |
| 47 bool execute; | |
| 48 int temp = 1; | |
| 49 // char *script = "'Hi' + 'Sara'"; | |
| 50 char threadScript[] = "'Hi ' + 'Sara!'"; | |
| 51 char *scr = threadScript; | |
| 52 JSString *outStr; | |
| 53 /* carries the value of the last expression of the script*/ | |
| 54 jsval rval1; | |
| 55 | |
| 56 /* The new context which is created by the thread*/ | |
| 57 JSContext *currContext; | |
| 58 /* pass the incoming argument of the function */ | |
| 59 // struct thread_parameter *arg1 = (struct thread_parameter *) arg; | |
| 60 //const char *uncScript = (const char*)arg->uncompiledScript; | |
| 61 //scr = (char *)arg; | |
| 62 /*Creates new context to be attached to the creator thread*/ | |
| 63 currContext = JS_NewContext(rt, 8192); | |
| 64 if (currContext == NULL) | |
| 65 return NULL; | |
| 66 /* In a thread safe build every entry to the api that uses | |
| 67 context must be in a request for thread safety*/ | |
| 68 JS_BeginRequest(currContext); | |
| 69 JS_SetOptions(currContext, JSOPTION_VAROBJFIX | JSOPTION_JIT | JSOPTION_METHODJIT); | |
| 70 JS_SetVersion(currContext, JSVERSION_LATEST); | |
| 71 /*Compiles the incoming script*/ | |
| 72 JSObject *compiledScript = JS_CompileScript(currContext, global, scr,strlen(scr), "threadFilename", 0); | |
| 73 /*Executes the compiled script*/ | |
| 74 execute = JS_ExecuteScript(currContext, global, compiledScript, &rval1); | |
| 75 if ( rval1 == JS_FALSE)//rval == JS_NULL | | |
| 76 return NULL; | |
| 77 /*gets the returned value of the last expression of the executed script*/ | |
| 78 outStr = JS_ValueToString(currContext, rval1); | |
| 79 printf("%s\n", JS_EncodeString(currContext, outStr)); | |
| 80 /*Ends the request */ | |
| 81 JS_EndRequest(currContext); | |
| 82 /*Destroy the context of that thread*/ | |
| 83 JS_DestroyContext(currContext); | |
| 84 //printf("HI, Sara"); | |
| 85 return NULL; | |
| 86 | |
| 87 } | |
| 88 /* | |
| 89 * | |
| 90 */ | |
| 91 int main(int argc, const char *argv[]) { | |
| 92 | |
| 93 /* JSAPI variables. */ | |
| 94 //JSRuntime *rt; | |
| 95 JSContext *cx; | |
| 96 // JSObject *global; | |
| 97 /* id of the created thread*/ | |
| 98 pthread_t thread_id; | |
| 99 | |
| 100 /* Create a JS runtime. You always need at least one runtime per process. */ | |
| 101 rt = JS_NewRuntime(8 * 1024 * 1024); | |
| 102 if (rt == NULL) | |
| 103 return 1; | |
| 104 | |
| 105 /* | |
| 106 * Create a context. You always need a context per thread. | |
| 107 * Note that this program is not multi-threaded. | |
| 108 This thread is used by the main thread only | |
| 109 */ | |
| 110 cx = JS_NewContext(rt, 8192); | |
| 111 if (cx == NULL) | |
| 112 return 1; | |
| 113 JS_BeginRequest(cx); | |
| 114 JS_SetOptions(cx, JSOPTION_VAROBJFIX | JSOPTION_JIT | JSOPTION_METHODJIT); | |
| 115 JS_SetVersion(cx, JSVERSION_LATEST); | |
| 116 JS_SetErrorReporter(cx, reportError); | |
| 117 | |
| 118 /* | |
| 119 * Create the global object in a new compartment. | |
| 120 * You always need a global object per context. | |
| 121 */ | |
| 122 | |
| 123 global = JS_NewCompartmentAndGlobalObject(cx, &global_class, NULL); | |
| 124 if (global == NULL) | |
| 125 return 1; | |
| 126 | |
| 127 /* | |
| 128 * Populate the global object with the standard JavaScript | |
| 129 * function and object classes, such as Object, Array, Date. | |
| 130 */ | |
| 131 if (!JS_InitStandardClasses(cx, global)) | |
| 132 return 1; | |
| 133 | |
| 134 /* Your application code here. This may include JSAPI calls | |
| 135 * to create your own custom JavaScript objects and to run scripts. | |
| 136 * | |
| 137 * The following example code creates a literal JavaScript script, | |
| 138 * evaluates it, and prints the result to stdout. | |
| 139 * | |
| 140 * Errors are conventionally saved in a JSBool variable named ok. | |
| 141 */ | |
| 142 | |
| 143 | |
| 144 /** Create the thread here....*/ | |
| 145 struct thread_parameter threadArg; | |
| 146 //threadArg.sharedGlobalObject = global; | |
| 147 char threadScript[] = "'Hi ' + 'Sara!'"; | |
| 148 //threadArg.tempstr = threadScript; | |
| 149 //threadArg.currRt = rt; | |
| 150 | |
| 151 pthread_create(&thread_id, NULL, &threadFunction,NULL); | |
| 152 /*****************************************************************/ | |
| 153 const char *script = "'Hello ' + 'World!'"; | |
| 154 jsval rval; | |
| 155 JSString *str; | |
| 156 JSBool ok; | |
| 157 const char *filename = "noname"; | |
| 158 uintN lineno = 0; | |
| 159 ok = JS_EvaluateScript(cx, global, script, strlen(script), | |
| 160 filename, lineno, &rval); | |
| 161 if ( rval == JS_FALSE)//rval == JS_NULL | | |
| 162 return 1; | |
| 163 str = JS_ValueToString(cx, rval); | |
| 164 printf("%s\n", JS_EncodeString(cx, str)); | |
| 165 | |
| 166 /* End of your application code */ | |
| 167 | |
| 168 /* Clean things up and shut down SpiderMonkey. */ | |
| 169 JS_EndRequest(cx); | |
| 170 JS_DestroyContext(cx); | |
| 171 JS_DestroyRuntime(rt); | |
| 172 JS_ShutDown(); | |
| 173 | |
| 174 return 0; | |
| 175 } | |
| 176 |
