Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Espresso, NodeJS 16.3 #60

Open
prepare opened this issue Sep 1, 2021 · 2 comments
Open

Espresso, NodeJS 16.3 #60

prepare opened this issue Sep 1, 2021 · 2 comments

Comments

@prepare
Copy link
Owner

prepare commented Sep 1, 2021

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;
using v8::Isolate;
using v8::Local;
using v8::Locker;

patch 1: src/node_main_instance.cc


about line 135

int NodeMainInstance::Run(const EnvSerializeInfo* env_info) {
  Locker locker(isolate_);
  Isolate::Scope isolate_scope(isolate_);
  HandleScope handle_scope(isolate_);

  int exit_code = 0;
  DeleteFnPtr<Environment, FreeEnvironment> env =
      CreateMainEnvironment(&exit_code, env_info);

  CHECK_NOT_NULL(env);
  {
    Context::Scope context_scope(env->context());

    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.get(), StartExecutionCallback{ 
          });
    
      exit_code = SpinEventLoop(env.get()).FromMaybe(1);
      //////////////////////////////////
      ////#espresso ,#3
      DoEngineClosingCallback(jsEngine, jscontext, exit_code);
      ////////////////////////////////
    }

patch 2&3: src/node_main_instance.cc


@prepare
Copy link
Owner Author

prepare commented Sep 1, 2021

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

about line 860

Module._resolveFilename = function(request, parent, isMain, options) {
  if (StringPrototypeStartsWith(request, 'node:') ||
      NativeModule.canBeRequiredByUsers(request)) {
    return request;
  }
    ////////////////////////////////////////////
    //#espresso, #4
    if (request.endsWith(".espr")) {
        return request;
    }
  ////////////////////////////////////////////
  let paths;

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


about line 1123

// Native extension for .js
Module._extensions['.js'] = function(module, filename) {
  if (StringPrototypeEndsWith(filename, '.js')) {
    const pkg = readPackageScope(filename);
    // Function require shouldn't be used in ES modules.
    if (pkg?.data?.type === 'module') {
      const parent = moduleParentCache.get(module);
      const parentPath = parent?.filename;
      const packageJsonPath = path.resolve(pkg.path, 'package.json');
      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
    }
  }
  // If already analyzed the source, then it will be cached.
  const cached = cjsParseCache.get(module);
  let content;
  if (cached?.source) {
    content = cached.source;
    cached.source = undefined;
  } else {
    content = fs.readFileSync(filename, 'utf8');
  }
  module._compile(content, filename);
};
//////////////////////////////////
//#espresso, #5 
Module._extensions['.espr'] = function (module, filename) {
    //this make node to callback to our module
    var content = LibEspresso.LoadMainSrcFile();
    module._compile(content, filename);
};
//////////////////////////////////

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


@prepare
Copy link
Owner Author

prepare commented Sep 1, 2021

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

'use strict';

const {
  prepareMainThreadExecution
} = require('internal/bootstrap/pre_execution');

prepareMainThreadExecution(true);

markBootstrapComplete();

// Note: this loads the module through the ESM loader if the module is
// determined to be an ES module. This hangs from the CJS module loader
// because we currently allow monkey-patching of the module loaders
// in the preloaded scripts through require('module').
// runMain here might be monkey-patched by users in --require.
// XXX: the monkey-patchability here should probably be deprecated.
 
//////////////////////////////////
//#espresso, #6 
/////////////////////////////////
if (typeof LibEspresso !== 'undefined') {
    //we use our lib espr LibEspresso
    require('internal/modules/cjs/loader').Module.runMain("hello.espr");
    return;
} 
/////////////////////////////////
require('internal/modules/cjs/loader').Module.runMain(process.argv[1]);

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant