Peer-reviewed code snippets that anyone can edit
A wiki for useful code snippets
jsandbox library
r1 r2
42
42
 
43
43
Library code:
44
44
[code=javascript]
45
45
/*
46
* jsandbox JavaScript Library v0.0.2
47
* 2009-06-19
46
* jsandbox JavaScript Library v0.1
47
* 2009-06-22
48
48
* By Elijah Grey, http://eligrey.com
49
49
*
50
50
* Public Domain.
51
51
* NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
52
52
*/
53
53
 
54
54
if (typeof document == "undefined") // loaded as a worker
55
55
        this.onmessage = function (event) { // recieved code
56
                var data = JSON.parse(event.data);
56
                var data = event.data;
57
57
               
58
58
                if (typeof data.code != "string")
59
59
                        throw new TypeError("code is not a string");
60
60
               
61
                postMessage(JSON.stringify(
62
                        (function() {
63
                                with({ // block access to XMLHttpRequest, importScripts, and postMessage for the sandboxed code
64
                                        window        : this, // provide a window object for compatibility with code that may use it
65
                                        self          : this,
66
                                        input         : this.input,
67
                                       
68
                                        importScripts : undefined,
69
                                        postMessage   : undefined,
70
                                        XMLHttpRequest: undefined,
71
                                                                               
72
                                        arguments     : undefined // dereference this function
73
                                })
74
                                        return eval("with({data:undefined}){eval("+JSON.stringify(data.code)+")}"); // dereference data object and eval data.code
75
                        }).call({input: data.input})
76
                ));
61
                postMessage((function() {
62
                        with({ // block access to XMLHttpRequest, importScripts, and postMessage for the sandboxed code
63
                                window        : this, // provide a window object for compatibility with code that may use it
64
                                self          : this,
65
                                input         : this.input,
66
                               
67
                                importScripts : undefined,
68
                                postMessage   : undefined,
69
                                XMLHttpRequest: undefined,
70
                                                                       
71
                                arguments     : undefined // dereference this function
72
                        })
73
                                return eval("with({data:undefined}){eval("+JSON.stringify(data.code)+")}"); // dereference data object and eval data.code
74
                                // the above line must be hand-minified if you minified the name of the data object
75
                }).call({input: data.input}));
77
76
        }; // you should terminate this worker now as it may have been tainted with new globals
78
77
 
79
78
else (function() { // loaded in a document
80
79
       
~
~
 
103
102
        [REQUIRED]  code     : Code to eval().
104
103
        [OPTIONAL]  input    : Input available to the code.
105
104
        [OPTIONAL]  callback : Callback to pass the return value of the eval() if no exceptions were thrown.
106
105
        [OPTIONAL]  onerror  : Callback to pass an exception if one is thrown opon eval()ing the code.
106
        [OPTIONAL]  uri      : URI of worker script. Defaults to jsandbox.uri.
107
107
        */
108
108
                var worker = new Worker(options.uri||jsandbox.uri),
109
109
                request    = { // request object
110
                        code: options.code // the only required option
110
                        code: options.code
111
111
                };
112
112
               
113
113
                if ("input" in options)
114
114
                        request.input = options.input;
115
115
               
116
116
                worker.onmessage = function(event) {
117
117
                        worker.terminate();
118
                        var data = event.data;
119
                        if (data !== undefined) // if response is not undefined, JSON.parse() it
120
                                data = JSON.parse(data);
121
118
                       
122
119
                        if (typeof options.callback == "function")
123
                                options.callback(data);
120
                                options.callback(event.data);
124
121
                };
125
122
                worker.onerror = function(e) {
126
123
                        worker.terminate();
124
                       
127
125
                        if (typeof options.onerror == "function")
128
126
                                options.onerror(e);
129
127
                };
130
128
 
131
                worker.postMessage(JSON.stringify(request)); // send and JSON.stringify() request object
129
                worker.postMessage(request); // send the request object
132
130
               
133
131
                return jsandbox;
134
132
        };
135
133
 
Tags added:
Tags removed: