github.com/igggame/nebulas-go@v2.1.0+incompatible/nf/nvm/v8/lib/execution_env.cc (about)

     1  // Copyright (C) 2017 go-nebulas authors
     2  //
     3  // This file is part of the go-nebulas library.
     4  //
     5  // the go-nebulas library is free software: you can redistribute it and/or
     6  // modify it under the terms of the GNU General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // the go-nebulas library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU General Public License
    16  // along with the go-nebulas library.  If not, see
    17  // <http://www.gnu.org/licenses/>.
    18  //
    19  
    20  #include "execution_env.h"
    21  #include "../engine.h"
    22  #include "file.h"
    23  #include "logger.h"
    24  #include "global.h"
    25  #include "string.h"
    26  static AttachLibVersionDelegate alvDelegate = NULL;
    27  
    28  int SetupExecutionEnv(Isolate *isolate, Local<Context> &context) {
    29    char *verlib = NULL;
    30    if (alvDelegate != NULL) {
    31      V8Engine *e = GetV8EngineInstance(context);
    32      verlib = alvDelegate(e, "lib/execution_env.js");
    33    }
    34    if (verlib == NULL) {
    35      return 1;
    36    }
    37  
    38    char path[64] = {0};
    39    strcat(path, verlib);
    40    free(verlib);
    41  
    42    char *data = readFile(path, NULL);
    43    // char *data = readFile("lib/execution_env.js", NULL);
    44    if (data == NULL) {
    45      isolate->ThrowException(Exception::Error(
    46          String::NewFromUtf8(isolate, "execution_env.js is not found.")));
    47      return 1;
    48    }
    49  
    50    Local<String> source =
    51        String::NewFromUtf8(isolate, data, NewStringType::kNormal)
    52            .ToLocalChecked();
    53    free(data);
    54  
    55    // Compile the source code.
    56    ScriptOrigin sourceSrcOrigin(
    57        String::NewFromUtf8(isolate, "execution_env.js"));
    58    MaybeLocal<Script> script =
    59        Script::Compile(context, source, &sourceSrcOrigin);
    60  
    61    if (script.IsEmpty()) {
    62      return 1;
    63    }
    64  
    65    // Run the script to get the result.
    66    MaybeLocal<Value> v = script.ToLocalChecked()->Run(context);
    67    if (v.IsEmpty()) {
    68      return 1;
    69    }
    70  
    71    return 0;
    72  }
    73  
    74  void InitializeExecutionEnvDelegate(AttachLibVersionDelegate aDelegate) {
    75    alvDelegate = aDelegate;
    76  }