Mercurial > spidermonkey-playground
diff tcb.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 | e4aec93c1e3c |
line wrap: on
line diff
--- a/tcb.cpp Thu Jun 25 17:53:28 2009 -0700 +++ b/tcb.cpp Thu Jun 25 18:11:01 2009 -0700 @@ -1,5 +1,15 @@ #include "tcb.h" +static JSFunctionSpec TCB_global_functions[] = { + JS_FS("print", TCB_print, 1, 0, 0), + JS_FS("stack", TCB_stack, 0, 0, 0), + JS_FS("lookupProperty", TCB_lookupProperty, 2, 0, 0), + JS_FS("functionInfo", TCB_functionInfo, 1, 0, 0), + JS_FS("enumerate", TCB_enumerate, 1, 0, 0), + JS_FS("forceGC", TCB_forceGC, 0, 0, 0), + JS_FS_END +}; + // The class of the global object. JSClass TCB_global_class = { "TCBGlobal", JSCLASS_GLOBAL_FLAGS, @@ -231,3 +241,35 @@ return JSTRAP_CONTINUE; } + +JSBool TCB_init(JSContext *cx, jsval *rval) +{ +#ifdef DEBUG + JS_SetGCZeal(cx, 2); +#endif + + JSRuntime *rt = JS_GetRuntime(cx); + + /* Create the TCB's global object. */ + JSObject *global = JS_NewObject(cx, &TCB_global_class, NULL, NULL); + if (global == NULL) { + JS_ReportOutOfMemory(cx); + return JS_FALSE; + } + + /* Populate the global object with the standard globals, + like Object and Array. */ + if (!JS_InitStandardClasses(cx, global) || + !JS_DefineFunctions(cx, global, TCB_global_functions)) + return JS_FALSE; + + if (!JS_SetThrowHook(rt, TCB_throwHook, global) || + !JS_DefineProperty(cx, global, "lastExceptionTraceback", JSVAL_NULL, + NULL, NULL, 0) || + !JS_DefineProperty(cx, global, "lastException", JSVAL_NULL, + NULL, NULL, 0)) + return JS_FALSE; + + *rval = OBJECT_TO_JSVAL(global); + return JS_TRUE; +}