From 039310270437369da8e1a67928d702520bdefddb Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Sun, 4 Nov 2018 23:46:50 +0100 Subject: [PATCH] Make the two runtimes capable of sharing the same example --- examples/HelloLoopery.js | 16 ---------------- examples/HelloWorld.js | 7 ++++--- src/host.cpp | 21 +++++++++++++++++---- 3 files changed, 21 insertions(+), 23 deletions(-) delete mode 100644 examples/HelloLoopery.js diff --git a/examples/HelloLoopery.js b/examples/HelloLoopery.js deleted file mode 100644 index b5deef3..0000000 --- a/examples/HelloLoopery.js +++ /dev/null @@ -1,16 +0,0 @@ -/* This is a script not for Node.js but runs completely stand-alone using loopery */ -print('Welcome to loopery!'); - -const port = 3000; - -App().get('/hello', (res, req) => { - res.end('world!'); -}).get('/*', (res, req) => { - res.writeHeader('content-type', 'text/html; charset= utf-8').end(req.getHeader('user-agent') + ' är din user agent, biatch!'); -}).listen(port, (token) => { - if (token) { - print('Listening to port ' + port); - } else { - print('Failed to listen to port ' + port); - } -}); diff --git a/examples/HelloWorld.js b/examples/HelloWorld.js index 982f4f9..5556cde 100644 --- a/examples/HelloWorld.js +++ b/examples/HelloWorld.js @@ -1,9 +1,9 @@ -const uWS = require('../dist/uws.js'); +/* The stand-alone runtime has uWS namespace already loaded. */ +var uWS = uWS ? uWS : require('../dist/uws.js'); +const world = 'Strings are slower than ArrayBuffer but who cares for demo purose!'; const port = 3000; -const world = new (require('util').TextEncoder)().encode('World!'); - uWS.App().get('/hello', (res, req) => { res.end(world); }).get('/*', (res, req) => { @@ -16,4 +16,5 @@ uWS.App().get('/hello', (res, req) => { } }); +/* This is not true for stand-alone */ console.log('Timers will not work until us_loop_integrate is done'); diff --git a/src/host.cpp b/src/host.cpp index 2bb691d..3787366 100644 --- a/src/host.cpp +++ b/src/host.cpp @@ -50,16 +50,29 @@ int main(int argc, char *argv[]) { // register functions (here is where addon.cpp comes in) Local global = ObjectTemplate::New(isolate); - // at least register print to global - global->Set(String::NewFromUtf8(isolate, "print", NewStringType::kNormal).ToLocalChecked(), + // add sign of stand-alone + global->Set(isolate, "runtime", String::NewFromUtf8(isolate, "µWebSockets.js", NewStringType::kNormal).ToLocalChecked()); + + // console namespace + Local console = ObjectTemplate::New(isolate); + // console.log + console->Set(String::NewFromUtf8(isolate, "log", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, print)); + // uWS namespace + Local uWS = ObjectTemplate::New(isolate); + + global->Set(String::NewFromUtf8(isolate, "console", NewStringType::kNormal).ToLocalChecked(), + console); + global->Set(String::NewFromUtf8(isolate, "uWS", NewStringType::kNormal).ToLocalChecked(), + uWS); + // vi skapar ett context som håller det globala objektet Local context = Context::New(isolate, nullptr, global); Context::Scope context_scope(context); - // register µWS features - Main(context->Global()); + // register µWS features under uWS namespace + Main(Local::Cast(context->Global()->Get(String::NewFromUtf8(isolate, "uWS", NewStringType::kNormal).ToLocalChecked()))); // ladda in scriptet (preprocessa include) Local source = String::NewFromUtf8(isolate, code.data(), NewStringType::kNormal, (int) code.length()).ToLocalChecked();