Make the two runtimes capable of sharing the same example

This commit is contained in:
Alex Hultman 2018-11-04 23:46:50 +01:00
parent 63101ad671
commit 0393102704
3 changed files with 21 additions and 23 deletions

View File

@ -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);
}
});

View File

@ -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');

View File

@ -50,16 +50,29 @@ int main(int argc, char *argv[]) {
// register functions (here is where addon.cpp comes in)
Local<ObjectTemplate> 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<ObjectTemplate> console = ObjectTemplate::New(isolate);
// console.log
console->Set(String::NewFromUtf8(isolate, "log", NewStringType::kNormal).ToLocalChecked(),
FunctionTemplate::New(isolate, print));
// uWS namespace
Local<ObjectTemplate> 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 = Context::New(isolate, nullptr, global);
Context::Scope context_scope(context);
// register µWS features
Main(context->Global());
// register µWS features under uWS namespace
Main(Local<Object>::Cast(context->Global()->Get(String::NewFromUtf8(isolate, "uWS", NewStringType::kNormal).ToLocalChecked())));
// ladda in scriptet (preprocessa include)
Local<String> source = String::NewFromUtf8(isolate, code.data(), NewStringType::kNormal, (int) code.length()).ToLocalChecked();