annotate CppApplication_1/first.cpp @ 3:b3ad79b3197c

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