Created
July 20, 2017 01:40
-
-
Save chenzhenjia/a98823e7a277ec2f02191efca972694d to your computer and use it in GitHub Desktop.
c++ v8 调用 JavaScript 函数
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include <v8.h> | |
#include <libplatform/libplatform.h> | |
using namespace v8; | |
using namespace std; | |
class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator { | |
public: | |
virtual void *Allocate(size_t length) { | |
void *data = AllocateUninitialized(length); | |
return data == NULL ? data : memset(data, 0, length); | |
} | |
virtual void *AllocateUninitialized(size_t length) { return malloc(length); } | |
virtual void Free(void *data, size_t) { free(data); } | |
}; | |
int main() { | |
fstream out; | |
out.open("/Users/chenzhenjia/workSpace/golang/src/weibo/assets/sina-encrypt.js", ios::in | ios::out); | |
char buffer[256]; | |
string js; | |
while (!out.eof()) { | |
out.getline(buffer, 256, '\n');//getline(char *,int,char) 表示该行字符达到256个或遇到换行就结束 | |
// cout << buffer << endl; | |
js.append(buffer); | |
} | |
out.close(); | |
// Initialize V8. | |
V8::InitializeICU(); | |
// V8::InitializeExternalStartupData(argv[0]); | |
Platform *platform = platform::CreateDefaultPlatform(); | |
V8::InitializePlatform(platform); | |
V8::Initialize(); | |
Isolate::CreateParams create_params; | |
ArrayBufferAllocator allocator; | |
create_params.array_buffer_allocator = &allocator; | |
Isolate *isolate = Isolate::New(create_params); | |
{ | |
Isolate::Scope isolate_scope(isolate); | |
// Create a stack-allocated handle scope. | |
HandleScope handle_scope(isolate); | |
// Create a new context. | |
Local<Context> context = Context::New(isolate); | |
// Enter the context for compiling and running the hello world script. | |
Context::Scope context_scope(context); | |
// Create a string containing the JavaScript source code. | |
Local<String> source = | |
String::NewFromUtf8(isolate, js.c_str(), | |
NewStringType::kNormal).ToLocalChecked(); | |
// Compile the source code. | |
Local<Script> script = Script::Compile(context, source).ToLocalChecked(); | |
script->Run(context).ToLocalChecked(); | |
const Local<Value> &value = context->Global()->Get(String::NewFromUtf8(isolate, "encrypt")); | |
Handle<Value> args[2]; | |
const Local<Function> &func = Handle<Function>::Cast(value); | |
args[0] = String::NewFromUtf8(isolate, | |
"EB2A38568661887FA180BDDB5CABD5F21C7BFD59C090CB2D245A87AC253062882729293E5506350508E7F9AA3BB77F4333231490F915F6D63C55FE2F08A49B353F444AD3993CACC02DB784ABBB8E42A9B1BBFFFB38BE18D78E87A0E41B9B8F73A928EE0CCEE1F6739884B9777E4FE9E88A1BBE495927AC4A799B3181D6442443"); | |
args[1] = String::NewFromUtf8(isolate, "12"); | |
Handle<Value> result = func->Call(context->Global(), 2, args); | |
// Run the script to get the result. | |
// Convert the result to an UTF8 string and print it. | |
String::Utf8Value utf8(result); | |
printf("%s\n", *utf8); | |
} | |
// Dispose the isolate and tear down V8. | |
isolate->Dispose(); | |
V8::Dispose(); | |
V8::ShutdownPlatform(); | |
delete platform; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment