annotate CppApplication_1 (copy)/first.cpp @ 5:a6cc4a802db6

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