diff --git a/examples/HelloLoopery.js b/examples/HelloLoopery.js new file mode 100644 index 0000000..b5deef3 --- /dev/null +++ b/examples/HelloLoopery.js @@ -0,0 +1,16 @@ +/* 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/host.pro b/host.pro new file mode 100644 index 0000000..0d8ad72 --- /dev/null +++ b/host.pro @@ -0,0 +1,31 @@ +TEMPLATE = app +CONFIG += console c++1z +CONFIG -= app_bundle +CONFIG -= qt + +SOURCES += \ + src/host.cpp \ + src/addon.cpp \ + uWebSockets/uSockets/src/eventing/epoll.c \ + uWebSockets/uSockets/src/context.c \ + uWebSockets/uSockets/src/socket.c \ + uWebSockets/uSockets/src/eventing/libuv.c \ + uWebSockets/uSockets/src/ssl.c \ + uWebSockets/uSockets/src/loop.c + + +#Separate the Node.js addon from host compilation +QMAKE_CXXFLAGS += -DADDON_IS_HOST + +INCLUDEPATH += /home/alexhultman/v8/v8/include +INCLUDEPATH += uWebSockets/src +INCLUDEPATH += uWebSockets/uSockets/src + +# We can link statically when I figure out how to compile V8 properly +LIBS += /home/alexhultman/v8/v8/out/x64.release/libv8_libplatform.so +LIBS += /home/alexhultman/v8/v8/out/x64.release/libv8_libbase.so +LIBS += /home/alexhultman/v8/v8/out/x64.release/libv8.so +LIBS += /home/alexhultman/v8/v8/out/x64.release/libicui18n.so +LIBS += /home/alexhultman/v8/v8/out/x64.release/libicuuc.so + +LIBS += -lssl -lcrypto diff --git a/src/host.cpp b/src/host.cpp new file mode 100644 index 0000000..2bb691d --- /dev/null +++ b/src/host.cpp @@ -0,0 +1,85 @@ +/* Welcome to the loopery host sources */ +#include +#include +using namespace v8; + +#include +#include +#include +#include + +#include "App.h" + +// We load features from the addon +extern void Main(Local exports); + +// console.log +void print(const FunctionCallbackInfo &args) { + String::Utf8Value utf8(args.GetIsolate(), args[0]); + std::cout << *utf8 << std::endl; +} + +int main(int argc, char *argv[]) { + + if (argc != 2) { + std::cout << "Usage: " << argv[0] << " " << std::endl; + return 0; + } + + // load script sources + std::ifstream t(argv[1]); + std::stringstream buffer; + buffer << t.rdbuf(); + std::string code = buffer.str(); + + // inte den blekaste + V8::InitializeICUDefaultLocation(argv[0]); + V8::InitializeExternalStartupData(argv[0]); + std::unique_ptr platform = platform::NewDefaultPlatform(); + V8::InitializePlatform(platform.get()); + V8::Initialize(); + + // isolate vet vi typ vad det är + Isolate::CreateParams create_params; + create_params.array_buffer_allocator = ArrayBuffer::Allocator::NewDefaultAllocator(); + Isolate *isolate = Isolate::New(create_params); + { + Isolate::Scope isolate_scope(isolate); + HandleScope handle_scope(isolate); + + // 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(), + FunctionTemplate::New(isolate, print)); + + // 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()); + + // ladda in scriptet (preprocessa include) + Local source = String::NewFromUtf8(isolate, code.data(), NewStringType::kNormal, (int) code.length()).ToLocalChecked(); + + // kompilera hela skiten, med allt inläst + Local