Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(937)

Side by Side Diff: src/v8c.cc

Issue 6070: Basic C bindings for v8.
Patch Set: Created 12 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/SConscript ('k') | v8c-test.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #define BUILDING_V8C
2 #include "../include/v8.h"
3 #include "../include/v8c.h"
4
5 template<typename T>
6 static V8Handle unwrap_handle(v8::Handle<T> handle) {
7 return *handle;
8 }
9
10 template<typename T>
11 static v8::Handle<T> wrap_handle(V8Handle handle) {
12 return v8::Handle<T>(reinterpret_cast<T*>(handle));
13 }
14
15 extern "C" {
16
17 bool v8_handle_is_empty(V8Handle handle) {
18 return wrap_handle<void>(handle).IsEmpty();
19 }
20
21 v8::HandleScope* v8_handle_scope_new() {
22 return new v8::HandleScope();
23 }
24
25 void v8_handle_scope_free(V8HandleScope* hs) {
26 delete hs;
27 }
28
29 V8Handle v8_script_compile(V8Handle code) {
30 return unwrap_handle(v8::Script::Compile(wrap_handle<v8::String>(code)));
31 }
32
33 V8Handle v8_script_run(V8Handle script) {
34 return unwrap_handle(wrap_handle<v8::Script>(script)->Run());
35 }
36
37 V8Handle v8_string_new_utf8(const char* data, int length) {
38 return unwrap_handle(v8::String::New(data, length));
39 }
40
41 int v8_string_length(V8Handle h) {
42 return wrap_handle<v8::String>(h)->Length();
43 }
44
45 V8StringUtf8Value* v8_string_utf8_value_new(V8Handle handle) {
46 return new v8::String::Utf8Value(wrap_handle<v8::Value>(handle));
47 }
48
49 int v8_string_utf8_value_length(V8StringUtf8Value* utf8) {
50 return utf8->length();
51 }
52
53 char* v8_string_utf8_value_chars(V8StringUtf8Value* utf8) {
54 return **utf8;
55 }
56
57 void v8_string_utf8_value_free(V8StringUtf8Value* utf8) {
58 delete utf8;
59 }
60
61 void v8_template_set(V8Handle tmpl, V8Handle name, V8Handle value) {
62 wrap_handle<v8::Template>(tmpl)->Set(wrap_handle<v8::String>(name),
63 wrap_handle<v8::Data>(value));
64 }
65
66 int v8_arguments_length(const V8Arguments* args) {
67 return args->Length();
68 }
69
70 V8Handle v8_arguments_get(const V8Arguments* args, int i) {
71 return unwrap_handle((*args)[i]);
72 }
73
74 struct V8InvocationCallbackData {
75 V8InvocationCallback callback;
76 // TODO: void* data.
77 };
78
79 static v8::Handle<v8::Value> v8_invocation_callback(const v8::Arguments& args) {
80 v8::Local<v8::External> data = v8::Local<v8::External>::Cast(args.Data());
81 V8InvocationCallbackData* callback_data =
82 static_cast<V8InvocationCallbackData*>(data->Value());
83 return wrap_handle<v8::Value>(callback_data->callback(&args));
84 }
85
86 V8Handle v8_function_template_new(V8InvocationCallback callback) {
87 // XXX this leaks. We need to somehow tie this lifetime to the
88 // function template's lifetime?
89 V8InvocationCallbackData* callback_data = new V8InvocationCallbackData;
90 callback_data->callback = callback;
91 return unwrap_handle(
92 v8::FunctionTemplate::New(v8_invocation_callback,
93 v8::External::New(callback_data)));
94 }
95
96 V8Handle v8_object_template_new() {
97 return unwrap_handle(v8::ObjectTemplate::New());
98 }
99
100 V8Handle v8_undefined() {
101 return unwrap_handle(v8::Undefined());
102 }
103
104 V8Handle v8_null() {
105 return unwrap_handle(v8::Null());
106 }
107
108 V8Handle v8_true() {
109 return unwrap_handle(v8::True());
110 }
111
112 V8Handle v8_false() {
113 return unwrap_handle(v8::False());
114 }
115
116 void v8_set_flags_from_command_line(int* argc, char** argv,
117 bool remove_flags) {
118 v8::V8::SetFlagsFromCommandLine(argc, argv, remove_flags);
119 }
120
121 V8TryCatch* v8_try_catch_new() {
122 return new V8TryCatch;
123 }
124
125 void v8_try_catch_free(V8TryCatch* try_catch) {
126 delete try_catch;
127 }
128
129 bool v8_try_catch_has_caught(V8TryCatch* try_catch) {
130 return try_catch->HasCaught();
131 }
132
133 V8Handle v8_try_catch_exception(V8TryCatch* try_catch) {
134 return unwrap_handle(try_catch->Exception());
135 }
136
137 V8Handle v8_try_catch_get_message(V8TryCatch* try_catch) {
138 return unwrap_handle(try_catch->Message());
139 }
140
141 void v8_try_catch_reset(V8TryCatch* try_catch) {
142 try_catch->Reset();
143 }
144
145 void v8_try_catch_set_verbose(V8TryCatch* try_catch, bool value) {
146 try_catch->SetVerbose(value);
147 }
148
149 V8Handle v8_context_new(V8ExtensionConfiguration extensions,
150 V8Handle global_template) {
151 return unwrap_handle(
152 v8::Context::New(extensions,
153 wrap_handle<v8::ObjectTemplate>(global_template)));
154 }
155
156 void v8_context_enter(V8Handle context) {
157 wrap_handle<v8::Context>(context)->Enter();
158 }
159
160 void v8_context_exit(V8Handle context) {
161 wrap_handle<v8::Context>(context)->Exit();
162 }
163
164 } // extern "C"
OLDNEW
« no previous file with comments | « src/SConscript ('k') | v8c-test.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698