Mercurial > spidermonkey-playground
comparison spidermonkey-playground.cpp @ 70:f6e3733eac01
Refactored some code, fixed some TODOs.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Thu, 25 Jun 2009 18:11:01 -0700 |
parents | be61430630ab |
children | b87b6ebb6e86 |
comparison
equal
deleted
inserted
replaced
69:be61430630ab | 70:f6e3733eac01 |
---|---|
149 // things too. | 149 // things too. |
150 return JS_LookupPropertyById(cx, obj, id, vp); | 150 return JS_LookupPropertyById(cx, obj, id, vp); |
151 } | 151 } |
152 | 152 |
153 static JSFunctionSpec tcb_global_functions[] = { | 153 static JSFunctionSpec tcb_global_functions[] = { |
154 JS_FS("print", TCB_print, 1, 0, 0), | |
155 JS_FS("stack", TCB_stack, 0, 0, 0), | |
156 JS_FS("lookupProperty", TCB_lookupProperty, 2, 0, 0), | |
157 JS_FS("functionInfo", TCB_functionInfo, 1, 0, 0), | |
158 JS_FS("enumerate", TCB_enumerate, 1, 0, 0), | |
159 JS_FS("forceGC", TCB_forceGC, 0, 0, 0), | |
160 JS_FS("getWrapper", getWrapper, 1, 0, 0), | 154 JS_FS("getWrapper", getWrapper, 1, 0, 0), |
161 JS_FS("unwrap", unwrapObject, 1, 0, 0), | 155 JS_FS("unwrap", unwrapObject, 1, 0, 0), |
162 JS_FS("wrap", wrapObject, 2, 0, 0), | 156 JS_FS("wrap", wrapObject, 2, 0, 0), |
163 JS_FS("require", require, 2, 0, 0), | 157 JS_FS("require", require, 2, 0, 0), |
164 JS_FS("profileMemory", profileMemory, 0, 0, 0), | 158 JS_FS("profileMemory", profileMemory, 0, 0, 0), |
191 JS_SetOptions(tcb_cx, JSOPTION_VAROBJFIX); | 185 JS_SetOptions(tcb_cx, JSOPTION_VAROBJFIX); |
192 JS_SetVersion(tcb_cx, JSVERSION_LATEST); | 186 JS_SetVersion(tcb_cx, JSVERSION_LATEST); |
193 | 187 |
194 JS_BeginRequest(tcb_cx); | 188 JS_BeginRequest(tcb_cx); |
195 | 189 |
196 #ifdef DEBUG | 190 jsval rval; |
197 JS_SetGCZeal(tcb_cx, 2); | 191 |
198 #endif | 192 if (!TCB_init(tcb_cx, &rval)) |
199 | 193 return 1; |
200 /* Create the TCB's global object. */ | 194 tcb_global = JSVAL_TO_OBJECT(rval); |
201 tcb_global = JS_NewObject(tcb_cx, &TCB_global_class, NULL, NULL); | |
202 if (tcb_global == NULL) | |
203 return 1; | |
204 | 195 |
205 JS_AddNamedRoot(tcb_cx, &tcb_global, "TCB Global"); | 196 JS_AddNamedRoot(tcb_cx, &tcb_global, "TCB Global"); |
206 | 197 |
207 /* Populate the tcb_global object with the standard globals, | |
208 like Object and Array. */ | |
209 if (!JS_InitStandardClasses(tcb_cx, tcb_global)) | |
210 return 1; | |
211 | |
212 if (!JS_DefineFunctions(tcb_cx, tcb_global, tcb_global_functions)) | 198 if (!JS_DefineFunctions(tcb_cx, tcb_global, tcb_global_functions)) |
213 return 1; | 199 return 1; |
214 | 200 |
215 // TODO: Check for return values here. | |
216 JS_SetThrowHook(rt, TCB_throwHook, tcb_global); | |
217 JS_DefineProperty(tcb_cx, tcb_global, "lastExceptionTraceback", JSVAL_NULL, | |
218 NULL, NULL, 0); | |
219 JS_DefineProperty(tcb_cx, tcb_global, "lastException", JSVAL_NULL, | |
220 NULL, NULL, 0); | |
221 | |
222 /* Your application code here. This may include JSAPI calls | |
223 to create your own custom JS objects and run scripts. */ | |
224 FILE *f = fopen(TCB_FILENAME, "r"); | 201 FILE *f = fopen(TCB_FILENAME, "r"); |
225 if (!f) | 202 if (!f) |
226 return 1; | 203 return 1; |
227 | 204 |
228 fseek(f, 0, SEEK_END); | 205 fseek(f, 0, SEEK_END); |
231 | 208 |
232 char source[fileSize]; | 209 char source[fileSize]; |
233 fread(source, fileSize, 1, f); | 210 fread(source, fileSize, 1, f); |
234 fclose(f); | 211 fclose(f); |
235 | 212 |
236 jsval rval; | |
237 if (!JS_EvaluateScript(tcb_cx, tcb_global, source, | 213 if (!JS_EvaluateScript(tcb_cx, tcb_global, source, |
238 fileSize, TCB_FILENAME, 1, | 214 fileSize, TCB_FILENAME, 1, |
239 &rval)) { | 215 &rval)) { |
240 jsval handleError; | 216 jsval handleError; |
241 if (JS_GetProperty(tcb_cx, tcb_global, "handleError", &handleError) && | 217 if (JS_GetProperty(tcb_cx, tcb_global, "handleError", &handleError) && |