forked from mattn/go-v8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathv8wrap.cc
221 lines (193 loc) · 5.81 KB
/
v8wrap.cc
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <v8.h>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include "v8wrap.h"
extern "C" {
char*
__strdup(const char* ptr) {
int l = strlen(ptr);
//char* p = (char*) malloc(l + 1);
char* p = new char[l + 1];
strcpy(p, ptr);
return p;
}
static volatile v8wrap_callback ___go_v8_callback = NULL;
static std::string
to_json(v8::Handle<v8::Value> value) {
v8::HandleScope scope;
v8::TryCatch try_catch;
v8::Handle<v8::Object> json = v8::Handle<v8::Object>::Cast(
v8::Context::GetCurrent()->Global()->Get(v8::String::New("JSON")));
v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(
json->GetRealNamedProperty(v8::String::New("stringify")));
v8::Handle<v8::Value> args[1];
args[0] = value;
v8::String::Utf8Value ret(
func->Call(v8::Context::GetCurrent()->Global(), 1, args)->ToString());
return (char*) *ret;
}
v8::Handle<v8::Value>
from_json(std::string str) {
v8::HandleScope scope;
v8::TryCatch try_catch;
v8::Handle<v8::Object> json = v8::Handle<v8::Object>::Cast(
v8::Context::GetCurrent()->Global()->Get(v8::String::New("JSON")));
v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(
json->GetRealNamedProperty(v8::String::New("parse")));
v8::Handle<v8::Value> args[1];
args[0] = v8::String::New(str.c_str());
return func->Call(v8::Context::GetCurrent()->Global(), 1, args);
}
v8data
v8_get_array_item(v8data* array, int index) {
return array[index];
}
v8::Handle<v8::Value>
_go_call(const v8::Arguments& args) {
v8::Locker v8Locker;
uint32_t id = args[0]->ToUint32()->Value();
v8::String::Utf8Value name(args[1]);
// Parse arguments
v8::Array* realArgs = v8::Array::Cast(*args[2]);
v8data* data = (v8data*) malloc(sizeof(v8data) * realArgs->Length());
for (int i = 0; i < realArgs->Length(); i++) {
v8::Local<v8::Value> arg = realArgs->Get(i);
v8::String::Utf8Value argString(arg);
if (arg->IsRegExp()) {
data[i].obj_type = v8regexp;
data[i].repr = __strdup(*argString);
} else if (arg->IsFunction()) {
data[i].obj_type = v8function;
data[i].repr = __strdup(*argString);
} else if (arg->IsNumber()) {
data[i].obj_type = v8number;
data[i].repr = __strdup(*argString);
} else if (arg->IsBoolean()) {
data[i].obj_type = v8boolean;
data[i].repr = __strdup(*argString);
} else {
data[i].obj_type = v8string;
data[i].repr = __strdup(to_json(arg).c_str());
}
}
v8::TryCatch try_catch;
char* retv;
retv = ___go_v8_callback(id, *name, data, realArgs->Length());
// Free args memory
for (int i = 0; i < realArgs->Length(); i++) {
free(data[i].repr);
}
free(data);
if (retv != NULL) {
v8::Handle<v8::Value> ret = from_json(retv);
free(retv);
return ret;
}
return v8::Undefined();
}
class V8Context {
public:
V8Context() : err_("") {
v8::Locker v8Locker;
v8::HandleScope scope;
global_ = v8::Persistent<v8::ObjectTemplate>::New(v8::ObjectTemplate::New());
global_->Set(v8::String::New("_go_call"),
v8::FunctionTemplate::New(_go_call));
v8::Handle<v8::Context> context = v8::Context::New(NULL, global_);
context_ = v8::Persistent<v8::Context>::New(context);
};
virtual ~V8Context() {
context_.Dispose();
global_.Dispose();
};
v8::Handle<v8::Context> context() { return context_; };
const char* err() const { return err_.c_str(); };
void err(const char* e) { this->err_ = std::string(e); }
private:
v8::Persistent<v8::ObjectTemplate> global_;
v8::Persistent<v8::Context> context_;
std::string err_;
};
void
v8_init(void *p) {
___go_v8_callback = (v8wrap_callback) p;
}
void*
v8_create() {
return (void*) new V8Context();
}
void
v8_release(void* ctx) {
delete static_cast<V8Context *>(ctx);
}
char*
v8_error(void* ctx) {
V8Context *context = static_cast<V8Context *>(ctx);
return __strdup(context->err());
}
static std::string
report_exception(v8::TryCatch& try_catch) {
v8::Handle<v8::Message> message = try_catch.Message();
v8::String::Utf8Value exception(try_catch.Exception());
std::stringstream ss;
if (message.IsEmpty()) {
ss << *exception << std::endl;
} else {
v8::String::Utf8Value filename(message->GetScriptResourceName());
const char* filename_string = *filename;
int linenum = message->GetLineNumber();
ss
<< filename_string
<< ":" << linenum
<< ": " << *exception << std::endl;
v8::String::Utf8Value sourceline(message->GetSourceLine());
ss << *sourceline << std::endl;
int start = message->GetStartColumn();
for (int n = 0; n < start; n++) {
ss << " ";
}
int end = message->GetEndColumn();
for (int n = start; n < end; n++) {
ss << "^";
}
ss << std::endl;
v8::String::Utf8Value stack_trace(try_catch.StackTrace());
if (stack_trace.length() > 0) {
const char* stack_trace_string = *stack_trace;
ss << stack_trace_string << std::endl;
}
}
return ss.str();
}
char*
v8_execute(void *ctx, char* source) {
v8::Locker v8Locker;
V8Context *context = static_cast<V8Context *>(ctx);
v8::HandleScope scope;
v8::TryCatch try_catch;
v8::Context::Scope context_scope(context->context());
context->err("");
v8::Handle<v8::Script> script
= v8::Script::Compile(v8::String::New(source), v8::Undefined());
if (script.IsEmpty()) {
v8::ThrowException(try_catch.Exception());
context->err(report_exception(try_catch).c_str());
return NULL;
} else {
v8::Handle<v8::Value> result = script->Run();
if (result.IsEmpty()) {
v8::ThrowException(try_catch.Exception());
context->err(report_exception(try_catch).c_str());
return NULL;
}
else if (result->IsFunction() || result->IsUndefined()) {
return __strdup("");
} else {
return __strdup(to_json(result).c_str());
}
}
}
}
// vim:set et sw=2 ts=2 ai: