File: binding.cc

package info (click to toggle)
nodejs 22.14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 246,928 kB
  • sloc: cpp: 1,582,349; javascript: 582,017; ansic: 82,400; python: 60,561; sh: 4,009; makefile: 2,263; asm: 1,732; pascal: 1,565; perl: 248; lisp: 222; xml: 42
file content (58 lines) | stat: -rw-r--r-- 1,595 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <node.h>
#include <node_buffer.h>
#include <assert.h>

namespace {

using v8::Context;
using v8::Isolate;
using v8::Local;
using v8::MaybeLocal;
using v8::Object;
using v8::Script;
using v8::String;
using v8::Value;

inline void MakeBufferInNewContext(
    const v8::FunctionCallbackInfo<v8::Value>& args) {
  Isolate* isolate = args.GetIsolate();
  Local<Context> context = Context::New(isolate);
  Context::Scope context_scope(context);

  // This should throw an exception, rather than actually do anything.
  MaybeLocal<Object> buf = node::Buffer::Copy(isolate, "foo", 3);
  assert(buf.IsEmpty());
}

inline void RunInNewContext(
    const v8::FunctionCallbackInfo<v8::Value>& args) {
  Isolate* isolate = args.GetIsolate();
  Local<Context> context = Context::New(isolate);
  Context::Scope context_scope(context);

  context->Global()->Set(
      context,
      String::NewFromUtf8(isolate, "data").ToLocalChecked(),
      args[1]).FromJust();

  assert(args[0]->IsString());  // source code
  Local<Script> script;
  Local<Value> ret;
  if (!Script::Compile(context, args[0].As<String>()).ToLocal(&script) ||
      !script->Run(context).ToLocal(&ret)) {
    return;
  }

  args.GetReturnValue().Set(ret);
}

inline void Initialize(Local<Object> exports,
                       Local<Value> module,
                       Local<Context> context) {
  NODE_SET_METHOD(exports, "runInNewContext", RunInNewContext);
  NODE_SET_METHOD(exports, "makeBufferInNewContext", MakeBufferInNewContext);
}

}  // anonymous namespace

NODE_MODULE_CONTEXT_AWARE(NODE_GYP_MODULE_NAME, Initialize)