prepare / Espresso

☕ Bridge your C# soul to the power of Javascript (V8 Javascript Engine / NodeJs)
MIT License
60 stars 11 forks source link

Espresso, NodeJS 16.8.0 #61

Open prepare opened 3 years ago

prepare commented 3 years ago

file src/node_main_instance.cc

about line 14

#if HAVE_INSPECTOR
#include "inspector/worker_inspector.h"  // ParentInspectorHandle
#endif

//////////////////////////////////
//#espresso ,#1
#include "../src/libespresso/bridge2.h"

void DoEngineSetupCallback(JsEngine* engine, JsContext* jsContext);
void DoEngineClosingCallback(JsEngine* engine,
                             JsContext* jsContext,
                             int exitCode);
//////////////////////////////////
namespace node {

using v8::Context;
using v8::HandleScope;

_patch 1: src/node_maininstance.cc


about line 151

 void NodeMainInstance::Run(int* exit_code, Environment* env) {
  if (*exit_code == 0) {
    /////////////////////////////
    //#espresso ,#2

    JsEngine* jsEngine = JsEngine::NewFromExistingIsolate(isolate_);
    v8::Persistent<Context>* pcontext =
        new v8::Persistent<Context>(isolate_, env->context());
    JsContext* jscontext =
        JsContext::NewFromExistingContext(0, jsEngine, pcontext);
    DoEngineSetupCallback(jsEngine, jscontext);

    ////////////////////////////////

    LoadEnvironment(env, StartExecutionCallback{});

    *exit_code = SpinEventLoop(env).FromMaybe(1);

    //////////////////////////////////
    ////#espresso ,#3
    DoEngineClosingCallback(jsEngine, jscontext, *exit_code);
    ///////////////////////////////
  }

  ResetStdio();

_patch 2&3: src/node_maininstance.cc


prepare commented 3 years ago

/lib/internal/modules/cjs/loader.js

about line 848


Module._resolveFilename = function(request, parent, isMain, options) {
  if (StringPrototypeStartsWith(request, 'node:') ||
      NativeModule.canBeRequiredByUsers(request)) {
    return request;
  }

    ////////////////////////////////////////////
    //#espresso, #4
    if (request.endsWith(".espr")) {
        return request;
    }
  ////////////////////////////////////////////

patch 4: /lib/internal/modules/cjs/loader.js


about line 1172


//////////////////////////////////
//#espresso, #5 
Module._extensions['.espr'] = function (module, filename) {
    //this make node to callback to our module
    var content = LibEspresso.LoadMainSrcFile();
    module._compile(content, filename);
};
//////////////////////////////////
// Native extension for .json
Module._extensions['.json'] = function(module, filename) {
  const content = fs.readFileSync(filename, 'utf8');

  if (policy?.manifest) {
    const moduleURL = pathToFileURL(filename);
    policy.manifest.assertIntegrity(moduleURL, content);
  }

patch 5: /lib/internal/modules/cjs/loader.js


prepare commented 3 years ago

file /lib/internal/main/run_main_module.js line 9

 markBootstrapComplete();
//////////////////////////////////
//#espresso, #6 
/////////////////////////////////
if (typeof LibEspresso !== 'undefined') {
    //we use our lib espr LibEspresso
    require('internal/modules/cjs/loader').Module.runMain("hello.espr");
    return;
}
/////////////////////////////////

patch 6: /lib/internal/main/run_main_module.js