Commit ffa6b5fe authored by binji's avatar binji Committed by Commit bot

Change d8 Worker API so it takes a string instead of a function.

This is more consistent with the DOM API, and is clearer w.r.t. which values
are available in the lexical environment of the Worker.

BUG=chromium:497295
R=jarin@chromium.org
LOG=n

Review URL: https://codereview.chromium.org/1218553004

Cr-Commit-Position: refs/heads/master@{#29426}
parent 2604c75e
......@@ -690,8 +690,8 @@ void Shell::Load(const v8::FunctionCallbackInfo<v8::Value>& args) {
void Shell::WorkerNew(const v8::FunctionCallbackInfo<v8::Value>& args) {
Isolate* isolate = args.GetIsolate();
HandleScope handle_scope(isolate);
if (args.Length() < 1 || !args[0]->IsFunction()) {
Throw(args.GetIsolate(), "1st argument must be function");
if (args.Length() < 1 || !args[0]->IsString()) {
Throw(args.GetIsolate(), "1st argument must be string");
return;
}
......@@ -703,12 +703,12 @@ void Shell::WorkerNew(const v8::FunctionCallbackInfo<v8::Value>& args) {
args.This()->SetInternalField(0, External::New(isolate, worker));
workers_.Add(worker);
String::Utf8Value function_string(args[0]->ToString());
if (!*function_string) {
Throw(args.GetIsolate(), "Function.prototype.toString failed");
String::Utf8Value script(args[0]);
if (!*script) {
Throw(args.GetIsolate(), "Can't get worker script");
return;
}
worker->StartExecuteInThread(isolate, *function_string);
worker->StartExecuteInThread(isolate, *script);
}
}
......@@ -1641,19 +1641,15 @@ Worker::Worker()
Worker::~Worker() { Cleanup(); }
void Worker::StartExecuteInThread(Isolate* isolate,
const char* function_string) {
DCHECK(base::NoBarrier_Load(&state_) == IDLE);
static const char format[] = "(%s).call(this);";
size_t len = strlen(function_string) + sizeof(format);
script_ = new char[len + 1];
i::Vector<char> vec(script_, static_cast<int>(len + 1));
i::SNPrintF(vec, format, function_string);
base::NoBarrier_Store(&state_, RUNNING);
thread_ = new WorkerThread(this);
thread_->Start();
void Worker::StartExecuteInThread(Isolate* isolate, const char* script) {
if (base::NoBarrier_CompareAndSwap(&state_, IDLE, RUNNING) == IDLE) {
script_ = i::StrDup(script);
thread_ = new WorkerThread(this);
thread_->Start();
} else {
// Somehow the Worker was started twice.
UNREACHABLE();
}
}
......
......@@ -239,7 +239,7 @@ class Worker {
Worker();
~Worker();
void StartExecuteInThread(Isolate* isolate, const char* function_string);
void StartExecuteInThread(Isolate* isolate, const char* script);
void PostMessage(SerializationData* data);
SerializationData* GetMessage();
void Terminate();
......
......@@ -27,27 +27,24 @@
// Flags: --harmony-sharedarraybuffer --harmony-atomics
function f() {
onmessage = function(m) {
var sab = m;
var ta = new Uint32Array(sab);
if (sab.byteLength !== 16) {
throw new Error("SharedArrayBuffer transfer byteLength");
}
for (var i = 0; i < 4; ++i) {
if (ta[i] !== i) {
throw new Error("SharedArrayBuffer transfer value " + i);
}
}
var workerScript =
`onmessage = function(m) {
var sab = m;
var ta = new Uint32Array(sab);
if (sab.byteLength !== 16) {
throw new Error('SharedArrayBuffer transfer byteLength');
}
for (var i = 0; i < 4; ++i) {
if (ta[i] !== i) {
throw new Error('SharedArrayBuffer transfer value ' + i);
}
}
// Atomically update ta[0]
Atomics.store(ta, 0, 100);
};
}
};`;
if (this.Worker) {
var w = new Worker(f);
var w = new Worker(workerScript);
var sab = new SharedArrayBuffer(16);
var ta = new Uint32Array(sab);
......
......@@ -26,21 +26,15 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
if (this.Worker) {
function f() {
var g = function () {
postMessage(42);
};
var workerScript =
`var w = new Worker('postMessage(42)');
onmessage = function(parentMsg) {
w.postMessage(parentMsg);
var childMsg = w.getMessage();
postMessage(childMsg);
};`;
var w = new Worker(g);
onmessage = function(parentMsg) {
w.postMessage(parentMsg);
var childMsg = w.getMessage();
postMessage(childMsg);
};
}
var w = new Worker(f);
var w = new Worker(workerScript);
w.postMessage(9);
assertEquals(42, w.getMessage());
}
......@@ -29,65 +29,64 @@
// spawns a new OS thread and isolate, and runs it concurrently with the
// current running thread.
function f() {
postMessage("Starting worker");
// Set a global variable; should not be visible outside of the worker's
// context.
foo = 100;
var c = 0;
onmessage = function(m) {
switch (c++) {
case 0:
if (m !== undefined) throw new Error("undefined");
break;
case 1:
if (m !== null) throw new Error("null");
break;
case 2:
if (m !== true) throw new Error("true");
break;
case 3:
if (m !== false) throw new Error("false");
break;
case 4:
if (m !== 100) throw new Error("Number");
break;
case 5:
if (m !== "hi") throw new Error("String");
break;
case 6:
if (JSON.stringify(m) !== '[4,true,"bye"]') throw new Error("Array");
break;
case 7:
if (JSON.stringify(m) !== '{"a":1,"b":2.5,"c":"three"}')
throw new Error("Object");
break;
case 8:
var ab = m;
var t = new Uint32Array(ab);
if (ab.byteLength !== 16)
throw new Error("ArrayBuffer clone byteLength");
for (var i = 0; i < 4; ++i)
if (t[i] !== i)
throw new Error("ArrayBuffer clone value " + i);
break;
case 9:
var ab = m;
var t = new Uint32Array(ab);
if (ab.byteLength !== 32)
throw new Error("ArrayBuffer transfer byteLength");
for (var i = 0; i < 8; ++i)
if (t[i] !== i)
throw new Error("ArrayBuffer transfer value " + i);
break;
}
if (c == 10) {
postMessage("DONE");
}
}
}
var workerScript =
`postMessage('Starting worker');
// Set a global variable; should not be visible outside of the worker's
// context.
foo = 100;
var c = 0;
onmessage = function(m) {
switch (c++) {
case 0:
if (m !== undefined) throw new Error('undefined');
break;
case 1:
if (m !== null) throw new Error('null');
break;
case 2:
if (m !== true) throw new Error('true');
break;
case 3:
if (m !== false) throw new Error('false');
break;
case 4:
if (m !== 100) throw new Error('Number');
break;
case 5:
if (m !== 'hi') throw new Error('String');
break;
case 6:
if (JSON.stringify(m) !== '[4,true,\"bye\"]') {
throw new Error('Array');
}
break;
case 7:
if (JSON.stringify(m) !== \"{'a':1,'b':2.5,'c':'three'}\")
throw new Error('Object');
break;
case 8:
var ab = m;
var t = new Uint32Array(ab);
if (ab.byteLength !== 16)
throw new Error('ArrayBuffer clone byteLength');
for (var i = 0; i < 4; ++i)
if (t[i] !== i)
throw new Error('ArrayBuffer clone value ' + i);
break;
case 9:
var ab = m;
var t = new Uint32Array(ab);
if (ab.byteLength !== 32)
throw new Error('ArrayBuffer transfer byteLength');
for (var i = 0; i < 8; ++i)
if (t[i] !== i)
throw new Error('ArrayBuffer transfer value ' + i);
break;
}
if (c == 10) {
postMessage('DONE');
}
};`;
if (this.Worker) {
......@@ -99,7 +98,7 @@ if (this.Worker) {
return ab;
}
var w = new Worker(f);
var w = new Worker(workerScript);
assertEquals("Starting worker", w.getMessage());
......@@ -132,8 +131,7 @@ if (this.Worker) {
// Make sure that the main thread doesn't block forever in getMessage() if
// the worker dies without posting a message.
function f2() {}
var w2 = new Worker(f2);
var w2 = new Worker('');
var msg = w2.getMessage();
assertEquals(undefined, msg);
}
......@@ -3,15 +3,12 @@
// found in the LICENSE file.
if (this.Worker) {
function __f_1() {
onmessage = function() {}
}
function __f_0(byteLength) {
var __v_1 = new ArrayBuffer(byteLength);
var __v_5 = new Uint32Array(__v_1);
return __v_5;
}
var __v_6 = new Worker(__f_1);
var __v_6 = new Worker('onmessage = function() {}');
var __v_3 = __f_0(16);
__v_6.postMessage(__v_3);
}
......@@ -5,6 +5,5 @@
// Flags: --invoke-weak-callbacks
if (this.Worker) {
function __f_1() {}
var __v_6 = new Worker(__f_1);
var __v_6 = new Worker('');
}
......@@ -8,8 +8,6 @@ if (this.Worker) {
this.l = [new __f_0, new __f_0];
}
__v_6 = new __f_1;
function __f_4() {
}
var __v_9 = new Worker(__f_4);
var __v_9 = new Worker('');
__v_9.postMessage(__v_6);
}
......@@ -4,7 +4,6 @@
if (this.Worker) {
__v_3 = "";
function __f_1() {}
var __v_6 = new Worker(__f_1);
var __v_6 = new Worker('');
__v_6.postMessage(__v_3);
}
......@@ -3,8 +3,7 @@
// found in the LICENSE file.
if (this.Worker) {
function __f_5() {}
var __v_10 = new Worker(__f_5);
var __v_10 = new Worker('');
__v_10.terminate();
__v_10.getMessage();
}
......@@ -5,6 +5,5 @@
// Flags: --no-test
if (this.Worker) {
function __f_4() {}
var __v_2 = new Worker(__f_4);
var __v_2 = new Worker('');
}
......@@ -5,5 +5,5 @@
if (this.Worker) {
Function.prototype.toString = "foo";
function __f_7() {}
assertThrows(function() { var __v_5 = new Worker(__f_7); });
assertThrows(function() { var __v_5 = new Worker(__f_7.toString()); });
}
......@@ -3,9 +3,6 @@
// found in the LICENSE file.
if (this.Worker) {
function __f_3() {
onmessage = function() {}
}
var __v_7 = new Worker(__f_3);
var __v_7 = new Worker('onmessage = function() {}');
__v_7.postMessage("");
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment