github.com/igggame/nebulas-go@v2.1.0+incompatible/nf/nvm/v8/lib/tracing.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 "tracing.h" 21 #include "logger.h" 22 #include "util.h" 23 24 #include <stdio.h> 25 #include <string.h> 26 27 #include <string> 28 29 extern void PrintException(Local<Context> context, TryCatch &trycatch); 30 31 static char inject_tracer_source_template[] = 32 "(function(){\n" 33 "const instCounter = require(\"instruction_counter.js\");\n" 34 "const source = \"%s\";\n" 35 "return instCounter.processScript(source, %d);\n" 36 "})();"; 37 38 int InjectTracingInstructionDelegate(char **result, Isolate *isolate, 39 const char *source, int source_line_offset, 40 Local<Context> context, TryCatch &trycatch, 41 void *delegateContext) { 42 TracingContext *tContext = static_cast<TracingContext *>(delegateContext); 43 tContext->tracable_source = NULL; 44 45 std::string s(source); 46 s = ReplaceAll(s, "\\", "\\\\"); 47 s = ReplaceAll(s, "\n", "\\n"); 48 s = ReplaceAll(s, "\r", "\\r"); 49 s = ReplaceAll(s, "\"", "\\\""); 50 51 char *injectTracerSource = NULL; 52 asprintf(&injectTracerSource, inject_tracer_source_template, s.c_str(), 53 tContext->strictDisallowUsage); 54 55 // Create a string containing the JavaScript source code. 56 Local<String> src = 57 String::NewFromUtf8(isolate, injectTracerSource, NewStringType::kNormal) 58 .ToLocalChecked(); 59 free(injectTracerSource); 60 61 // Compile the source code. 62 ScriptOrigin sourceSrcOrigin( 63 String::NewFromUtf8(isolate, "_inject_tracer.js"), 64 Integer::New(isolate, source_line_offset)); 65 MaybeLocal<Script> script = Script::Compile(context, src, &sourceSrcOrigin); 66 67 if (script.IsEmpty()) { 68 PrintException(context, trycatch); 69 return 1; 70 } 71 72 // Run the script to get the result. 73 MaybeLocal<Value> ret = script.ToLocalChecked()->Run(context); 74 if (ret.IsEmpty()) { 75 PrintException(context, trycatch); 76 return 1; 77 } 78 79 Local<Value> checked_ret = ret.ToLocalChecked(); 80 if (!checked_ret->IsObject()) { 81 return 1; 82 } 83 84 Local<Object> obj = Local<Object>::Cast(checked_ret); 85 Local<Value> traceableSource = 86 obj->Get(String::NewFromUtf8(isolate, "traceableSource")); 87 Local<Value> lineOffset = 88 obj->Get(String::NewFromUtf8(isolate, "lineOffset")); 89 90 if (!traceableSource->IsString() || !lineOffset->IsNumber()) { 91 LogErrorf("instruction_counter.js:processScript() should return object " 92 "with traceableSource and lineOffset keys."); 93 return 1; 94 } 95 96 String::Utf8Value str(traceableSource); 97 tContext->tracable_source = (char *)malloc(str.length() + 1); 98 strcpy(tContext->tracable_source, *str); 99 100 tContext->source_line_offset = (int)lineOffset->IntegerValue(); 101 102 return 0; 103 }