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

     1  #include "random.h"
     2  #include "global.h"
     3  #include "../engine.h"
     4  #include "instruction_counter.h"
     5  #include "logger.h"
     6  
     7  static GetTxRandomFunc sRandomDelegate = NULL;
     8  
     9  // void NewNativeRandomFunction(Isolate *isolate,
    10  //                               Local<ObjectTemplate> globalTpl) {
    11  //   globalTpl->Set(String::NewFromUtf8(isolate, "_native_random"),
    12  //                  FunctionTemplate::New(isolate, RandomCallback),
    13  //                  static_cast<PropertyAttribute>(PropertyAttribute::DontDelete |
    14  //                                                 PropertyAttribute::ReadOnly));
    15  // }
    16  
    17  void NewRandomInstance(Isolate *isolate, Local<Context> context,
    18                             void *handler) {
    19    Local<ObjectTemplate> blockTpl = ObjectTemplate::New(isolate);
    20  //   Local<Object> blockTpl = context->Global();
    21    blockTpl->SetInternalFieldCount(1);
    22  
    23    blockTpl->Set(String::NewFromUtf8(isolate, "random"),
    24                  FunctionTemplate::New(isolate, RandomCallback),
    25                  static_cast<PropertyAttribute>(PropertyAttribute::DontDelete |
    26                                                 PropertyAttribute::ReadOnly));
    27  
    28    Local<Object> instance = blockTpl->NewInstance(context).ToLocalChecked();
    29    instance->SetInternalField(0, External::New(isolate, handler));
    30    
    31    context->Global()->DefineOwnProperty(
    32        context, String::NewFromUtf8(isolate, "_native_math"), instance,
    33        static_cast<PropertyAttribute>(PropertyAttribute::DontDelete |
    34                                       PropertyAttribute::ReadOnly | PropertyAttribute::DontEnum));
    35  }
    36  
    37  void RandomCallback(const v8::FunctionCallbackInfo<v8::Value> &info) {
    38    Isolate *isolate = info.GetIsolate();
    39    Local<Object> thisArg = info.Holder();
    40    Local<External> handler = Local<External>::Cast(thisArg->GetInternalField(0));
    41    if (info.Length() != 0) {
    42      isolate->ThrowException(
    43          Exception::Error(String::NewFromUtf8(isolate, "require random args err")));
    44      return;
    45    }
    46  //   size_t cnt = 0;
    47  
    48    // char *value = sRandomDelegate(handler->Value());
    49    // if (value == NULL) {
    50    //   info.GetReturnValue().SetNull();
    51    // } else {
    52    //   info.GetReturnValue().Set(String::NewFromUtf8(isolate, value));
    53    //   free(value);
    54    // }
    55    size_t cnt = 0;
    56    char *result = NULL;
    57    char *exceptionInfo = NULL;
    58  
    59    int err = sRandomDelegate(handler->Value(), &cnt, &result, &exceptionInfo);
    60    DEAL_ERROR_FROM_GOLANG(err);
    61  
    62    if (result != NULL) {
    63      free(result);
    64    }
    65  
    66    if (exceptionInfo != NULL) {
    67      free(exceptionInfo);
    68    }
    69    // record storage usage.
    70    IncrCounter(isolate, isolate->GetCurrentContext(), cnt);
    71  }
    72  
    73  void InitializeRandom(GetTxRandomFunc delegate) {
    74    sRandomDelegate = delegate;
    75  }