annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
18
33b2d70d8ada Added license blocks.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
1 /* ***** BEGIN LICENSE BLOCK *****
33b2d70d8ada Added license blocks.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
33b2d70d8ada Added license blocks.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
3 *
33b2d70d8ada Added license blocks.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
4 * The contents of this file are subject to the Mozilla Public License Version
33b2d70d8ada Added license blocks.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
5 * 1.1 (the "License"); you may not use this file except in compliance with
33b2d70d8ada Added license blocks.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
6 * the License. You may obtain a copy of the License at
33b2d70d8ada Added license blocks.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
7 * http://www.mozilla.org/MPL/
33b2d70d8ada Added license blocks.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
8 *
33b2d70d8ada Added license blocks.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
9 * Software distributed under the License is distributed on an "AS IS" basis,
33b2d70d8ada Added license blocks.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
33b2d70d8ada Added license blocks.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
11 * for the specific language governing rights and limitations under the
33b2d70d8ada Added license blocks.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
12 * License.
33b2d70d8ada Added license blocks.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
13 *
33b2d70d8ada Added license blocks.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
14 * The Original Code is Ubiquity.
33b2d70d8ada Added license blocks.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
15 *
33b2d70d8ada Added license blocks.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
16 * The Initial Developer of the Original Code is Mozilla.
33b2d70d8ada Added license blocks.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
17 * Portions created by the Initial Developer are Copyright (C) 2007
33b2d70d8ada Added license blocks.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
18 * the Initial Developer. All Rights Reserved.
33b2d70d8ada Added license blocks.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
19 *
33b2d70d8ada Added license blocks.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
20 * Contributor(s):
33b2d70d8ada Added license blocks.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
21 * Atul Varma <atul@mozilla.com>
33b2d70d8ada Added license blocks.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
22 *
33b2d70d8ada Added license blocks.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
23 * Alternatively, the contents of this file may be used under the terms of
33b2d70d8ada Added license blocks.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
33b2d70d8ada Added license blocks.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
33b2d70d8ada Added license blocks.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
26 * in which case the provisions of the GPL or the LGPL are applicable instead
33b2d70d8ada Added license blocks.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
27 * of those above. If you wish to allow use of your version of this file only
33b2d70d8ada Added license blocks.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
28 * under the terms of either the GPL or the LGPL, and not to allow others to
33b2d70d8ada Added license blocks.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
29 * use your version of this file under the terms of the MPL, indicate your
33b2d70d8ada Added license blocks.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
30 * decision by deleting the provisions above and replace them with the notice
33b2d70d8ada Added license blocks.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
31 * and other provisions required by the GPL or the LGPL. If you do not delete
33b2d70d8ada Added license blocks.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
32 * the provisions above, a recipient may use your version of this file under
33b2d70d8ada Added license blocks.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
33 * the terms of any one of the MPL, the GPL or the LGPL.
33b2d70d8ada Added license blocks.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
34 *
33b2d70d8ada Added license blocks.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
35 * ***** END LICENSE BLOCK ***** */
33b2d70d8ada Added license blocks.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
36
23
a94e7cc0727a Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents: 20
diff changeset
37 // This file is based on the code in the JSAPI User Guide:
a94e7cc0727a Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents: 20
diff changeset
38 //
a94e7cc0727a Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents: 20
diff changeset
39 // https://developer.mozilla.org/En/SpiderMonkey/JSAPI_User_Guide
a94e7cc0727a Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents: 20
diff changeset
40
1
7444443d2646 added simple script and wrapper.cpp from jetpack
Atul Varma <varmaa@toolness.com>
parents: 0
diff changeset
41 #include "string.h"
0
d14fb1d5326c Origination
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
42 #include "jsapi.h"
4
71de19be1054 Added a native stack() function.
Atul Varma <varmaa@toolness.com>
parents: 3
diff changeset
43 #include "jsdbgapi.h"
0
d14fb1d5326c Origination
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
44
69
be61430630ab Separated out some of the TCB-specific code into tcb.cpp/h.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
45 #include "tcb.h"
1
7444443d2646 added simple script and wrapper.cpp from jetpack
Atul Varma <varmaa@toolness.com>
parents: 0
diff changeset
46 #include "wrapper.h"
40
f86740dc5fa0 Added an almost-completely-unimplemented TCP server socket object that uses NSPR.
Atul Varma <varmaa@toolness.com>
parents: 39
diff changeset
47 #include "server_socket.h"
50
853f80bd3b4b Added tentative profileMemory() server, which suspends the current JS runtime, creates a new one and runs a web server in it.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
48 #include "memory_profiler.h"
1
7444443d2646 added simple script and wrapper.cpp from jetpack
Atul Varma <varmaa@toolness.com>
parents: 0
diff changeset
49
26
3c3ee9206419 Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 23
diff changeset
50 // The name of our JS script containing our Trusted Code Base (TCB).
2
1f3e9c8df4f0 Script is now read from tcb.js.
Atul Varma <varmaa@toolness.com>
parents: 1
diff changeset
51 #define TCB_FILENAME "tcb.js"
1f3e9c8df4f0 Script is now read from tcb.js.
Atul Varma <varmaa@toolness.com>
parents: 1
diff changeset
52
26
3c3ee9206419 Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 23
diff changeset
53 // Global references to our TCB.
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.
Atul Varma <varmaa@toolness.com>
parents: 9
diff changeset
54 static JSContext *tcb_cx;
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.
Atul Varma <varmaa@toolness.com>
parents: 9
diff changeset
55 static JSObject *tcb_global;
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.
Atul Varma <varmaa@toolness.com>
parents: 9
diff changeset
56
69
be61430630ab Separated out some of the TCB-specific code into tcb.cpp/h.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
57 // The class of SecurableModules.
be61430630ab Separated out some of the TCB-specific code into tcb.cpp/h.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
58 static JSClass SM_global_class = {
be61430630ab Separated out some of the TCB-specific code into tcb.cpp/h.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
59 "SecurableModuleGlobal", JSCLASS_GLOBAL_FLAGS,
0
d14fb1d5326c Origination
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
60 JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
d14fb1d5326c Origination
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
61 JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
d14fb1d5326c Origination
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
62 JSCLASS_NO_OPTIONAL_MEMBERS
d14fb1d5326c Origination
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
63 };
d14fb1d5326c Origination
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
64
23
a94e7cc0727a Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents: 20
diff changeset
65 // This native JS function is an implementation of the SecurableModule
a94e7cc0727a Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents: 20
diff changeset
66 // require() function. For more information, see:
a94e7cc0727a Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents: 20
diff changeset
67 //
a94e7cc0727a Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents: 20
diff changeset
68 // https://wiki.mozilla.org/ServerJS/Modules/SecurableModules
a94e7cc0727a Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents: 20
diff changeset
69 //
a94e7cc0727a Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents: 20
diff changeset
70 // The function takes two parameters: a filename to import, and a JS
a94e7cc0727a Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents: 20
diff changeset
71 // object containing objects to export into the module. The latter is
a94e7cc0727a Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents: 20
diff changeset
72 // accessible from the loaded module via a global called 'imports'.
a94e7cc0727a Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents: 20
diff changeset
73 //
a94e7cc0727a Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents: 20
diff changeset
74 // This function returns the SecurableModule's 'exports' global.
a94e7cc0727a Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents: 20
diff changeset
75
6
500e267ed094 Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
76 static JSBool require(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
500e267ed094 Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
77 jsval *rval)
500e267ed094 Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
78 {
500e267ed094 Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
79 char *filename;
500e267ed094 Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
80 JSObject *exports;
500e267ed094 Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
81
500e267ed094 Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
82 if (!JS_ConvertArguments(cx, argc, argv, "so", &filename, &exports))
500e267ed094 Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
83 return JS_FALSE;
500e267ed094 Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
84
500e267ed094 Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
85 FILE *f = fopen(filename, "r");
500e267ed094 Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
86 if (!f) {
500e267ed094 Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
87 JS_ReportError(cx, "File not found");
500e267ed094 Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
88 return JS_FALSE;
500e267ed094 Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
89 }
500e267ed094 Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
90
39
067371eb9601 The playground now only allocates buffers the size of the JS files being loaded, rather than 100k ones.
Atul Varma <varmaa@toolness.com>
parents: 38
diff changeset
91 fseek(f, 0, SEEK_END);
067371eb9601 The playground now only allocates buffers the size of the JS files being loaded, rather than 100k ones.
Atul Varma <varmaa@toolness.com>
parents: 38
diff changeset
92 long fileSize = ftell(f);
067371eb9601 The playground now only allocates buffers the size of the JS files being loaded, rather than 100k ones.
Atul Varma <varmaa@toolness.com>
parents: 38
diff changeset
93 fseek(f, 0, SEEK_SET);
067371eb9601 The playground now only allocates buffers the size of the JS files being loaded, rather than 100k ones.
Atul Varma <varmaa@toolness.com>
parents: 38
diff changeset
94
067371eb9601 The playground now only allocates buffers the size of the JS files being loaded, rather than 100k ones.
Atul Varma <varmaa@toolness.com>
parents: 38
diff changeset
95 char source[fileSize];
067371eb9601 The playground now only allocates buffers the size of the JS files being loaded, rather than 100k ones.
Atul Varma <varmaa@toolness.com>
parents: 38
diff changeset
96 fread(source, fileSize, 1, f);
6
500e267ed094 Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
97 fclose(f);
500e267ed094 Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
98
16
36b66c249446 Added some TODOs.
Atul Varma <varmaa@toolness.com>
parents: 15
diff changeset
99 // TODO: Check for return values here.
6
500e267ed094 Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
100 JSContext *module_cx = JS_NewContext(JS_GetRuntime(cx), 8192);
64
6a7e87cd1588 Fixed a TODO; executing the whole code now works, though we get GC root leak warnings.
Atul Varma <varmaa@toolness.com>
parents: 63
diff changeset
101 JS_BeginRequest(module_cx);
69
be61430630ab Separated out some of the TCB-specific code into tcb.cpp/h.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
102 JSObject *module_global = JS_NewObject(module_cx,
be61430630ab Separated out some of the TCB-specific code into tcb.cpp/h.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
103 &SM_global_class, NULL,
16
36b66c249446 Added some TODOs.
Atul Varma <varmaa@toolness.com>
parents: 15
diff changeset
104 NULL);
6
500e267ed094 Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
105 JS_InitStandardClasses(module_cx, module_global);
500e267ed094 Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
106 JS_DefineProperty(module_cx, module_global, "imports",
500e267ed094 Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
107 OBJECT_TO_JSVAL(exports), NULL, NULL, 0);
16
36b66c249446 Added some TODOs.
Atul Varma <varmaa@toolness.com>
parents: 15
diff changeset
108 JSObject *module_exports = JS_NewObject(module_cx, NULL, NULL,
36b66c249446 Added some TODOs.
Atul Varma <varmaa@toolness.com>
parents: 15
diff changeset
109 module_global);
6
500e267ed094 Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
110 JS_DefineProperty(module_cx, module_global, "exports",
500e267ed094 Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
111 OBJECT_TO_JSVAL(module_exports), NULL, NULL, 0);
500e267ed094 Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
112
500e267ed094 Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
113 jsval module_rval;
500e267ed094 Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
114 if (!JS_EvaluateScript(module_cx, module_global, source,
500e267ed094 Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
115 strlen(source), filename, 1, &module_rval)) {
64
6a7e87cd1588 Fixed a TODO; executing the whole code now works, though we get GC root leak warnings.
Atul Varma <varmaa@toolness.com>
parents: 63
diff changeset
116 JS_EndRequest(module_cx);
6
500e267ed094 Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
117 JS_DestroyContext(module_cx);
500e267ed094 Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
118 return JS_FALSE;
500e267ed094 Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
119 }
500e267ed094 Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
120
500e267ed094 Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
121 *rval = OBJECT_TO_JSVAL(module_exports);
500e267ed094 Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
122
64
6a7e87cd1588 Fixed a TODO; executing the whole code now works, though we get GC root leak warnings.
Atul Varma <varmaa@toolness.com>
parents: 63
diff changeset
123 JS_EndRequest(module_cx);
6
500e267ed094 Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
124 JS_DestroyContext(module_cx);
500e267ed094 Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
125
500e267ed094 Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
126 return JS_TRUE;
500e267ed094 Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
127 }
500e267ed094 Added a really simple securableModule require() implementation.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
128
23
a94e7cc0727a Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents: 20
diff changeset
129 // Our global checkAccess callback just checks to see if a JS function called
a94e7cc0727a Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents: 20
diff changeset
130 // 'checkAccess' has been defined in the TCB, and delegates to that if so. If
a94e7cc0727a Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents: 20
diff changeset
131 // not, though, we do a default checkAccess.
a94e7cc0727a Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents: 20
diff changeset
132
9
fcefc3e5a9df Added a runtime access check callback.
Atul Varma <varmaa@toolness.com>
parents: 8
diff changeset
133 static JSBool checkAccess(JSContext *cx, JSObject *obj, jsval id,
fcefc3e5a9df Added a runtime access check callback.
Atul Varma <varmaa@toolness.com>
parents: 8
diff changeset
134 JSAccessMode mode, jsval *vp)
fcefc3e5a9df Added a runtime access check callback.
Atul Varma <varmaa@toolness.com>
parents: 8
diff changeset
135 {
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.
Atul Varma <varmaa@toolness.com>
parents: 9
diff changeset
136 jsval checkAccess;
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.
Atul Varma <varmaa@toolness.com>
parents: 9
diff changeset
137 if (tcb_global && JS_GetProperty(tcb_cx, tcb_global, "checkAccess",
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.
Atul Varma <varmaa@toolness.com>
parents: 9
diff changeset
138 &checkAccess) &&
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.
Atul Varma <varmaa@toolness.com>
parents: 9
diff changeset
139 JSVAL_IS_OBJECT(checkAccess) &&
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.
Atul Varma <varmaa@toolness.com>
parents: 9
diff changeset
140 JS_ObjectIsFunction(tcb_cx, JSVAL_TO_OBJECT(checkAccess))) {
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.
Atul Varma <varmaa@toolness.com>
parents: 9
diff changeset
141 jsval argv[2];
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.
Atul Varma <varmaa@toolness.com>
parents: 9
diff changeset
142 argv[0] = OBJECT_TO_JSVAL(obj);
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.
Atul Varma <varmaa@toolness.com>
parents: 9
diff changeset
143 argv[1] = id;
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.
Atul Varma <varmaa@toolness.com>
parents: 9
diff changeset
144 return JS_CallFunctionValue(tcb_cx, tcb_global, checkAccess, 2, argv,
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.
Atul Varma <varmaa@toolness.com>
parents: 9
diff changeset
145 vp);
9
fcefc3e5a9df Added a runtime access check callback.
Atul Varma <varmaa@toolness.com>
parents: 8
diff changeset
146 }
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.
Atul Varma <varmaa@toolness.com>
parents: 9
diff changeset
147
23
a94e7cc0727a Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents: 20
diff changeset
148 // TODO: This doesn't work for the 'caller' attribute, and possibly other
a94e7cc0727a Added some documentation to spidermonkey-playground.cpp.
Atul Varma <varmaa@toolness.com>
parents: 20
diff changeset
149 // things too.
9
fcefc3e5a9df Added a runtime access check callback.
Atul Varma <varmaa@toolness.com>
parents: 8
diff changeset
150 return JS_LookupPropertyById(cx, obj, id, vp);
fcefc3e5a9df Added a runtime access check callback.
Atul Varma <varmaa@toolness.com>
parents: 8
diff changeset
151 }
fcefc3e5a9df Added a runtime access check callback.
Atul Varma <varmaa@toolness.com>
parents: 8
diff changeset
152
3
7237a81919b2 Renamed some variables.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
153 static JSFunctionSpec tcb_global_functions[] = {
40
f86740dc5fa0 Added an almost-completely-unimplemented TCP server socket object that uses NSPR.
Atul Varma <varmaa@toolness.com>
parents: 39
diff changeset
154 JS_FS("getWrapper", getWrapper, 1, 0, 0),
f86740dc5fa0 Added an almost-completely-unimplemented TCP server socket object that uses NSPR.
Atul Varma <varmaa@toolness.com>
parents: 39
diff changeset
155 JS_FS("unwrap", unwrapObject, 1, 0, 0),
f86740dc5fa0 Added an almost-completely-unimplemented TCP server socket object that uses NSPR.
Atul Varma <varmaa@toolness.com>
parents: 39
diff changeset
156 JS_FS("wrap", wrapObject, 2, 0, 0),
f86740dc5fa0 Added an almost-completely-unimplemented TCP server socket object that uses NSPR.
Atul Varma <varmaa@toolness.com>
parents: 39
diff changeset
157 JS_FS("require", require, 2, 0, 0),
50
853f80bd3b4b Added tentative profileMemory() server, which suspends the current JS runtime, creates a new one and runs a web server in it.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
158 JS_FS("profileMemory", profileMemory, 0, 0, 0),
41
f495677654fc renamed Socket to ServerSocket.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
159 JS_FS("ServerSocket", createServerSocket, 0, 0, 0),
1
7444443d2646 added simple script and wrapper.cpp from jetpack
Atul Varma <varmaa@toolness.com>
parents: 0
diff changeset
160 JS_FS_END
7444443d2646 added simple script and wrapper.cpp from jetpack
Atul Varma <varmaa@toolness.com>
parents: 0
diff changeset
161 };
7444443d2646 added simple script and wrapper.cpp from jetpack
Atul Varma <varmaa@toolness.com>
parents: 0
diff changeset
162
9
fcefc3e5a9df Added a runtime access check callback.
Atul Varma <varmaa@toolness.com>
parents: 8
diff changeset
163 static JSSecurityCallbacks securityCallbacks = {
fcefc3e5a9df Added a runtime access check callback.
Atul Varma <varmaa@toolness.com>
parents: 8
diff changeset
164 checkAccess,
fcefc3e5a9df Added a runtime access check callback.
Atul Varma <varmaa@toolness.com>
parents: 8
diff changeset
165 NULL,
fcefc3e5a9df Added a runtime access check callback.
Atul Varma <varmaa@toolness.com>
parents: 8
diff changeset
166 NULL
fcefc3e5a9df Added a runtime access check callback.
Atul Varma <varmaa@toolness.com>
parents: 8
diff changeset
167 };
fcefc3e5a9df Added a runtime access check callback.
Atul Varma <varmaa@toolness.com>
parents: 8
diff changeset
168
0
d14fb1d5326c Origination
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
169 int main(int argc, const char *argv[])
d14fb1d5326c Origination
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
170 {
d14fb1d5326c Origination
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
171 /* JS variables. */
d14fb1d5326c Origination
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
172 JSRuntime *rt;
d14fb1d5326c Origination
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
173
d14fb1d5326c Origination
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
174 /* Create a JS runtime. */
d14fb1d5326c Origination
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
175 rt = JS_NewRuntime(8L * 1024L * 1024L);
d14fb1d5326c Origination
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
176 if (rt == NULL)
d14fb1d5326c Origination
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
177 return 1;
d14fb1d5326c Origination
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
178
9
fcefc3e5a9df Added a runtime access check callback.
Atul Varma <varmaa@toolness.com>
parents: 8
diff changeset
179 JS_SetRuntimeSecurityCallbacks(rt, &securityCallbacks);
fcefc3e5a9df Added a runtime access check callback.
Atul Varma <varmaa@toolness.com>
parents: 8
diff changeset
180
0
d14fb1d5326c Origination
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
181 /* Create a context. */
3
7237a81919b2 Renamed some variables.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
182 tcb_cx = JS_NewContext(rt, 8192);
7237a81919b2 Renamed some variables.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
183 if (tcb_cx == NULL)
0
d14fb1d5326c Origination
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
184 return 1;
3
7237a81919b2 Renamed some variables.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
185 JS_SetOptions(tcb_cx, JSOPTION_VAROBJFIX);
7237a81919b2 Renamed some variables.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
186 JS_SetVersion(tcb_cx, JSVERSION_LATEST);
0
d14fb1d5326c Origination
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
187
58
0b66a265df13 Fixed some bugs that raised assertions in debug builds of SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents: 51
diff changeset
188 JS_BeginRequest(tcb_cx);
0b66a265df13 Fixed some bugs that raised assertions in debug builds of SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents: 51
diff changeset
189
70
f6e3733eac01 Refactored some code, fixed some TODOs.
Atul Varma <varmaa@toolness.com>
parents: 69
diff changeset
190 jsval rval;
63
4910bc49a182 set GC Zeal to 2 and fixed some bugs exposed by it.
Atul Varma <varmaa@toolness.com>
parents: 58
diff changeset
191
70
f6e3733eac01 Refactored some code, fixed some TODOs.
Atul Varma <varmaa@toolness.com>
parents: 69
diff changeset
192 if (!TCB_init(tcb_cx, &rval))
0
d14fb1d5326c Origination
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
193 return 1;
70
f6e3733eac01 Refactored some code, fixed some TODOs.
Atul Varma <varmaa@toolness.com>
parents: 69
diff changeset
194 tcb_global = JSVAL_TO_OBJECT(rval);
0
d14fb1d5326c Origination
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
195
58
0b66a265df13 Fixed some bugs that raised assertions in debug builds of SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents: 51
diff changeset
196 JS_AddNamedRoot(tcb_cx, &tcb_global, "TCB Global");
51
8750e9ecf3af Added a getGCRoots() function.
Atul Varma <varmaa@toolness.com>
parents: 50
diff changeset
197
3
7237a81919b2 Renamed some variables.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
198 if (!JS_DefineFunctions(tcb_cx, tcb_global, tcb_global_functions))
1
7444443d2646 added simple script and wrapper.cpp from jetpack
Atul Varma <varmaa@toolness.com>
parents: 0
diff changeset
199 return 1;
7444443d2646 added simple script and wrapper.cpp from jetpack
Atul Varma <varmaa@toolness.com>
parents: 0
diff changeset
200
2
1f3e9c8df4f0 Script is now read from tcb.js.
Atul Varma <varmaa@toolness.com>
parents: 1
diff changeset
201 FILE *f = fopen(TCB_FILENAME, "r");
1f3e9c8df4f0 Script is now read from tcb.js.
Atul Varma <varmaa@toolness.com>
parents: 1
diff changeset
202 if (!f)
1f3e9c8df4f0 Script is now read from tcb.js.
Atul Varma <varmaa@toolness.com>
parents: 1
diff changeset
203 return 1;
39
067371eb9601 The playground now only allocates buffers the size of the JS files being loaded, rather than 100k ones.
Atul Varma <varmaa@toolness.com>
parents: 38
diff changeset
204
067371eb9601 The playground now only allocates buffers the size of the JS files being loaded, rather than 100k ones.
Atul Varma <varmaa@toolness.com>
parents: 38
diff changeset
205 fseek(f, 0, SEEK_END);
067371eb9601 The playground now only allocates buffers the size of the JS files being loaded, rather than 100k ones.
Atul Varma <varmaa@toolness.com>
parents: 38
diff changeset
206 long fileSize = ftell(f);
067371eb9601 The playground now only allocates buffers the size of the JS files being loaded, rather than 100k ones.
Atul Varma <varmaa@toolness.com>
parents: 38
diff changeset
207 fseek(f, 0, SEEK_SET);
2
1f3e9c8df4f0 Script is now read from tcb.js.
Atul Varma <varmaa@toolness.com>
parents: 1
diff changeset
208
39
067371eb9601 The playground now only allocates buffers the size of the JS files being loaded, rather than 100k ones.
Atul Varma <varmaa@toolness.com>
parents: 38
diff changeset
209 char source[fileSize];
067371eb9601 The playground now only allocates buffers the size of the JS files being loaded, rather than 100k ones.
Atul Varma <varmaa@toolness.com>
parents: 38
diff changeset
210 fread(source, fileSize, 1, f);
2
1f3e9c8df4f0 Script is now read from tcb.js.
Atul Varma <varmaa@toolness.com>
parents: 1
diff changeset
211 fclose(f);
1f3e9c8df4f0 Script is now read from tcb.js.
Atul Varma <varmaa@toolness.com>
parents: 1
diff changeset
212
3
7237a81919b2 Renamed some variables.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
213 if (!JS_EvaluateScript(tcb_cx, tcb_global, source,
50
853f80bd3b4b Added tentative profileMemory() server, which suspends the current JS runtime, creates a new one and runs a web server in it.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
214 fileSize, TCB_FILENAME, 1,
1
7444443d2646 added simple script and wrapper.cpp from jetpack
Atul Varma <varmaa@toolness.com>
parents: 0
diff changeset
215 &rval)) {
8
e14f496e6e08 added a handleError() function to the TCB, which is called whenever an unhandled exception occurs. Also fixed a bug in the setting of lastException.
Atul Varma <varmaa@toolness.com>
parents: 6
diff changeset
216 jsval handleError;
e14f496e6e08 added a handleError() function to the TCB, which is called whenever an unhandled exception occurs. Also fixed a bug in the setting of lastException.
Atul Varma <varmaa@toolness.com>
parents: 6
diff changeset
217 if (JS_GetProperty(tcb_cx, tcb_global, "handleError", &handleError) &&
e14f496e6e08 added a handleError() function to the TCB, which is called whenever an unhandled exception occurs. Also fixed a bug in the setting of lastException.
Atul Varma <varmaa@toolness.com>
parents: 6
diff changeset
218 JSVAL_IS_OBJECT(handleError) &&
e14f496e6e08 added a handleError() function to the TCB, which is called whenever an unhandled exception occurs. Also fixed a bug in the setting of lastException.
Atul Varma <varmaa@toolness.com>
parents: 6
diff changeset
219 JS_ObjectIsFunction(tcb_cx, JSVAL_TO_OBJECT(handleError))) {
e14f496e6e08 added a handleError() function to the TCB, which is called whenever an unhandled exception occurs. Also fixed a bug in the setting of lastException.
Atul Varma <varmaa@toolness.com>
parents: 6
diff changeset
220 JS_CallFunctionValue(tcb_cx, tcb_global, handleError, 0, NULL, &rval);
e14f496e6e08 added a handleError() function to the TCB, which is called whenever an unhandled exception occurs. Also fixed a bug in the setting of lastException.
Atul Varma <varmaa@toolness.com>
parents: 6
diff changeset
221 }
1
7444443d2646 added simple script and wrapper.cpp from jetpack
Atul Varma <varmaa@toolness.com>
parents: 0
diff changeset
222 return 1;
7444443d2646 added simple script and wrapper.cpp from jetpack
Atul Varma <varmaa@toolness.com>
parents: 0
diff changeset
223 }
0
d14fb1d5326c Origination
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
224
d14fb1d5326c Origination
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
225 /* Cleanup. */
58
0b66a265df13 Fixed some bugs that raised assertions in debug builds of SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents: 51
diff changeset
226 JS_RemoveRoot(tcb_cx, &tcb_global);
0b66a265df13 Fixed some bugs that raised assertions in debug builds of SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents: 51
diff changeset
227 JS_EndRequest(tcb_cx);
3
7237a81919b2 Renamed some variables.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
228 JS_DestroyContext(tcb_cx);
0
d14fb1d5326c Origination
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
229 JS_DestroyRuntime(rt);
d14fb1d5326c Origination
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
230 JS_ShutDown();
d14fb1d5326c Origination
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
231
d14fb1d5326c Origination
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
232 printf("Farewell.\n");
d14fb1d5326c Origination
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
233 return 0;
d14fb1d5326c Origination
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
234 }