From b9377891a034ad093b8c6f7c067a0298d7e0b16b Mon Sep 17 00:00:00 2001 From: kriskowal Date: Mon, 3 Jan 2011 12:29:13 -0800 Subject: [PATCH] Since workers are already isolated by processes, they need not and should not be separated by contexts. The wrapped context limits the ability to pass the global worker object as an argument and for primordials to be shared between the require context and the worker context. --- examples/trivial/master.js | 8 ++++++++ examples/trivial/worker.js | 6 ++++++ lib/webworker-child.js | 4 ++-- 3 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 examples/trivial/master.js create mode 100644 examples/trivial/worker.js diff --git a/examples/trivial/master.js b/examples/trivial/master.js new file mode 100644 index 0000000..97fc2a1 --- /dev/null +++ b/examples/trivial/master.js @@ -0,0 +1,8 @@ +var Worker = require("webworker").Worker; +var worker = new Worker(__dirname + "/worker.js"); +worker.onmessage = function (message) { + console.log(message.data); + worker.terminate(); +}; +worker.postMessage("Hello, World!"); + diff --git a/examples/trivial/worker.js b/examples/trivial/worker.js new file mode 100644 index 0000000..963c04c --- /dev/null +++ b/examples/trivial/worker.js @@ -0,0 +1,6 @@ +var worker = this; +worker.onmessage = function (message) { + // echo + worker.postMessage(message.data); +} + diff --git a/lib/webworker-child.js b/lib/webworker-child.js index 34b3c9f..29a77ba 100644 --- a/lib/webworker-child.js +++ b/lib/webworker-child.js @@ -125,7 +125,7 @@ ws.addListener('open', function() { process.addListener('uncaughtException', exceptionHandler); // Execute the worker - scriptObj.runInNewContext(workerCtx); + scriptObj.runInThisContext(); }); // Construt the Script object to host the worker's code @@ -145,7 +145,7 @@ default: } // Set up the context for the worker instance -var workerCtx = {}; +var workerCtx = global; // Context elements required for node.js //