Mercurial > spidermonkey-playground
diff spidermonkey-playground.cpp @ 10:16a605ff036c
The TCB can now define a global checkAccess() handler. It also has access to a lookupProperty() function that can retrieve an attribute of an object without initiating security checks or property getters.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Fri, 19 Jun 2009 14:44:39 -0700 |
parents | fcefc3e5a9df |
children | e14f433f3a58 |
line wrap: on
line diff
--- a/spidermonkey-playground.cpp Fri Jun 19 12:50:42 2009 -0700 +++ b/spidermonkey-playground.cpp Fri Jun 19 14:44:39 2009 -0700 @@ -7,6 +7,9 @@ #define MAX_SCRIPT_SIZE 100000 #define TCB_FILENAME "tcb.js" +static JSContext *tcb_cx; +static JSObject *tcb_global; + /* The class of the global object. */ static JSClass global_class = { "global", JSCLASS_GLOBAL_FLAGS, @@ -87,6 +90,22 @@ return JS_TRUE; } +static JSBool lookupProperty(JSContext *cx, JSObject *obj, uintN argc, + jsval *argv, jsval *rval) +{ + JSObject *target; + + if (argc < 2) { + JS_ReportError(cx, "Must provide id to lookup."); + return JS_FALSE; + } + + if (!JS_ConvertArguments(cx, argc, argv, "o", &target)) + return JS_FALSE; + + return JS_LookupPropertyById(cx, target, argv[1], rval); +} + static JSBool stack(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { @@ -191,14 +210,18 @@ static JSBool checkAccess(JSContext *cx, JSObject *obj, jsval id, JSAccessMode mode, jsval *vp) { - if (JSVAL_IS_STRING(id)) { - char *str; - if (!JS_ConvertArguments(cx, 1, &id, "s", &str)) { - printf("checkAccess() string conversion failed!\n"); - return JS_FALSE; - } - printf("checkAccess: %s\n", str); + jsval checkAccess; + if (tcb_global && JS_GetProperty(tcb_cx, tcb_global, "checkAccess", + &checkAccess) && + JSVAL_IS_OBJECT(checkAccess) && + JS_ObjectIsFunction(tcb_cx, JSVAL_TO_OBJECT(checkAccess))) { + jsval argv[2]; + argv[0] = OBJECT_TO_JSVAL(obj); + argv[1] = id; + return JS_CallFunctionValue(tcb_cx, tcb_global, checkAccess, 2, argv, + vp); } + return JS_LookupPropertyById(cx, obj, id, vp); } @@ -207,6 +230,7 @@ JS_FS("wrap", wrap, 2, 0, 0), JS_FS("stack", stack, 0, 0, 0), JS_FS("require", require, 2, 0, 0), + JS_FS("lookupProperty", lookupProperty, 2, 0, 0), JS_FS_END }; @@ -220,8 +244,6 @@ { /* JS variables. */ JSRuntime *rt; - JSContext *tcb_cx; - JSObject *tcb_global; /* Create a JS runtime. */ rt = JS_NewRuntime(8L * 1024L * 1024L);