Skip to content
This repository was archived by the owner on Jul 3, 2020. It is now read-only.
This repository was archived by the owner on Jul 3, 2020. It is now read-only.

Evaluate in different context #152

@RossComputerGuy

Description

@RossComputerGuy

Recently, I've been messing around with the Runtime.js kernel. I've created a syscall that can allow a script to be evaluated in its own context. Here is the code:

NATIVE_FUNCTION(NativesObject,EvalContext) {
  PROLOGUE_NOTHIS;
  USEARG(0);
  USEARG(1);
  USEARG(2);
  VALIDATEARG(1, STRING, "eval: argument 1 is not a string");
  
  if(!arg0->IsObject()) THROW_ERROR("argument 0 is not an object");
  v8::Local<v8::Object> globals = arg0.As<v8::Object>();
  v8::Local<v8::ObjectTemplate> global { v8::ObjectTemplate::New(iv8) };
  v8::Local<v8::Context> ctx = v8::Context::New(iv8,nullptr,global);
  v8::Context::Scope cs(ctx);
  ctx->Global()->Set(ctx,v8::String::NewFromUtf8(iv8,"global",v8::NewStringType::kNormal).ToLocalChecked(),globals);

  v8::ScriptOrigin origin((!arg2.IsEmpty() && arg2->IsString()) ? arg2.As<v8::String>() : v8::String::Empty(iv8));

  v8::Local<v8::String> source_code = arg1.As<v8::String>();
  v8::ScriptCompiler::Source source(source_code,origin);
  v8::MaybeLocal<v8::Script> maybe_script = v8::ScriptCompiler::Compile(ctx,&source,v8::ScriptCompiler::CompileOptions::kNoCompileOptions);

  v8::Local<v8::Script> script;
  if(!maybe_script.ToLocal(&script)) return;

  RT_ASSERT(!script.IsEmpty());
  v8::MaybeLocal<v8::Value> maybe_result = script->Run(ctx);
  v8::Local<v8::Value> result;
  if(!maybe_result.ToLocal(&result)) return;

  RT_ASSERT(!result.IsEmpty());
  args.GetReturnValue().Set(result);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions