Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 253 lines (217 sloc) 7.351 kb
7399428 @philogb Voila
authored
1 /*
2 * v8-gl.cpp
3 *
4 */
5
6 #include "v8-gl.h"
14e7a34 @cscott Nicer exception reporting (filename, linenumber, position).
cscott authored
7 #include <assert.h>
34db969 @philogb First three examples
authored
8 #include <stdio.h>
50431dc @philogb Added a load function to load js files. Clean up.
authored
9 #include <string>
88b085b @philogb Added functions to load shaders from files and added a texture
authored
10 #include <string.h>
7399428 @philogb Voila
authored
11
ef34aef @cscott Hook up v8-typed-array to v8-gl.
cscott authored
12 extern "C" void
13 v8_typed_array_init (Handle<Object> target);
14
50431dc @philogb Added a load function to load js files. Clean up.
authored
15 Persistent<Context> V8GL::context;
b773f5c @philogb added a log function callable from JS
authored
16
50431dc @philogb Added a load function to load js files. Clean up.
authored
17 //UTILITY FUNCTIONS
18 // Reads a file into a v8 string.
19 Handle<String> ReadFile(const string& name) {
20 FILE* file = fopen(name.c_str(), "rb");
21 if (file == NULL) return Handle<String>();
b773f5c @philogb added a log function callable from JS
authored
22
50431dc @philogb Added a load function to load js files. Clean up.
authored
23 fseek(file, 0, SEEK_END);
24 int size = ftell(file);
25 rewind(file);
7399428 @philogb Voila
authored
26
50431dc @philogb Added a load function to load js files. Clean up.
authored
27 char* chars = new char[size + 1];
28 chars[size] = '\0';
29 for (int i = 0; i < size;) {
30 int read = fread(&chars[i], 1, size - i, file);
31 i += read;
32 }
33 fclose(file);
34 Handle<String> result = String::New(chars, size);
35 delete[] chars;
36 return result;
37 }
38
39 bool exec(string file) {
7399428 @philogb Voila
authored
40 HandleScope handle_scope;
50431dc @philogb Added a load function to load js files. Clean up.
authored
41 Handle<String> source = ReadFile(file);
42
43 if (source.IsEmpty()) {
44 fprintf(stderr, "Error reading '%s'.\n", file.c_str());
45 return false;
46 }
7399428 @philogb Voila
authored
47
48 // We're just about to compile the script; set up an error handler to
49 // catch any exceptions the script might throw.
50 TryCatch try_catch;
51
52 // Compile the script and check for errors.
14e7a34 @cscott Nicer exception reporting (filename, linenumber, position).
cscott authored
53 Handle<Script> compiled_script = Script::Compile(source,
54 String::New(file.c_str()));
7399428 @philogb Voila
authored
55 if (compiled_script.IsEmpty()) {
14e7a34 @cscott Nicer exception reporting (filename, linenumber, position).
cscott authored
56 V8GL::ReportException(&try_catch);
50431dc @philogb Added a load function to load js files. Clean up.
authored
57 // The script failed to compile; bail out.
58 return false;
7399428 @philogb Voila
authored
59 }
60
61 // Run the script!
62 Handle<Value> result = compiled_script->Run();
63 if (result.IsEmpty()) {
50431dc @philogb Added a load function to load js files. Clean up.
authored
64 // The TryCatch above is still in effect and will have caught the error.
14e7a34 @cscott Nicer exception reporting (filename, linenumber, position).
cscott authored
65 assert(try_catch.HasCaught());
66 V8GL::ReportException(&try_catch);
50431dc @philogb Added a load function to load js files. Clean up.
authored
67 // Running the script failed; bail out.
68 return false;
7399428 @philogb Voila
authored
69 }
70 return true;
71 }
72
14e7a34 @cscott Nicer exception reporting (filename, linenumber, position).
cscott authored
73 // nicer exception handling (borrowed from d8)
74 const char* V8GL::ToCString(const v8::String::Utf8Value& value) {
75 return *value ? *value : "<string conversion failed>";
76 }
77 void V8GL::ReportException(v8::TryCatch* try_catch) {
78 HandleScope handle_scope;
79 v8::String::Utf8Value exception(try_catch->Exception());
80 const char* exception_string = ToCString(exception);
81 Handle<Message> message = try_catch->Message();
82 if (message.IsEmpty()) {
83 // V8 didn't provide any extra information about this error; just
84 // print the exception.
85 printf("%s\n", exception_string);
86 } else {
87 // Print (filename):(line number): (message).
88 v8::String::Utf8Value filename(message->GetScriptResourceName());
89 const char* filename_string = ToCString(filename);
90 int linenum = message->GetLineNumber();
91 printf("%s:%i: %s\n", filename_string, linenum, exception_string);
92 // Print line of source code.
93 v8::String::Utf8Value sourceline(message->GetSourceLine());
94 const char* sourceline_string = ToCString(sourceline);
95 printf("%s\n", sourceline_string);
96 // Print wavy underline (GetUnderline is deprecated).
97 int start = message->GetStartColumn();
98 for (int i = 0; i < start; i++) {
99 printf(" ");
100 }
101 int end = message->GetEndColumn();
102 for (int i = start; i < end; i++) {
103 printf("^");
104 }
105 printf("\n");
106 v8::String::Utf8Value stack_trace(try_catch->StackTrace());
107 if (stack_trace.length() > 0) {
108 const char* stack_trace_string = ToCString(stack_trace);
109 printf("%s\n", stack_trace_string);
110 }
111 }
112 }
50431dc @philogb Added a load function to load js files. Clean up.
authored
113
114 //FUNCTIONS IN GLOBAL SCOPE
115
116 //prints a string to stdout
117 Handle<Value> log(const Arguments& args) {
118 //if less that nbr of formal parameters then do nothing
119 if (args.Length() < 1) return v8::Undefined();
120 //define handle scope
121 HandleScope scope;
122 //get arguments
123 String::Utf8Value value0(args[0]);
124 char* arg0 = *value0;
125
126 //make call
127 fprintf(stdout, "%s", arg0);
128 return v8::Undefined();
129 }
130
7b06957 @cscott Add 'read' built-in to get file contents as a string.
cscott authored
131 //loads a text file
132 Handle<Value> read(const Arguments& args) {
133 if (args.Length() < 1) return v8::Undefined();
134 //define handle scope
135 HandleScope scope;
136 //get arguments
137 String::Utf8Value value0(args[0]);
138 char* arg0 = *value0;
139 string str(V8GLUtils::getRealPath(arg0));
140 Handle<String> result = ReadFile(str);
141 if (result.IsEmpty()) {
142 fprintf(stderr, "Error reading '%s'.\n", str.c_str());
143 return ThrowException(String::New("Could not open file."));
144 }
145 return scope.Close(result);
146 }
147
50431dc @philogb Added a load function to load js files. Clean up.
authored
148 //loads a js file
149 Handle<Value> load(const Arguments& args) {
150 //if less that nbr of formal parameters then do nothing
151 int len = args.Length();
152 int i;
153
154 if (len < 1) return v8::Undefined();
155 //define handle scope
156 HandleScope scope;
157
158 // Enter the new context so all the following operations take place
159 // within it.
160 Context::Scope context_scope(V8GL::context);
161
162 for (i = 0; i < len; ++i) {
163 //get argument
164 String::Utf8Value value0(args[i]);
165 char* arg0 = *value0;
7602942 @cscott Set root path to reflect the current javascript file when using load().
cscott authored
166 char* filepath = V8GLUtils::getRealPath(arg0);
167
168 char *old_path = V8GLUtils::pushRootPath(filepath);
169 bool success = exec(string(filepath));
170 V8GLUtils::popRootPath(old_path);
171
172 if(!success) {
50431dc @philogb Added a load function to load js files. Clean up.
authored
173 fprintf(stderr, "Error reading '%s'.\n", arg0);
7602942 @cscott Set root path to reflect the current javascript file when using load().
cscott authored
174 return ThrowException(String::New("Failed to load script"));
50431dc @philogb Added a load function to load js files. Clean up.
authored
175 }
176 }
177
178 return v8::Undefined();
179 }
180
181 bool V8GL::executeScript(string file) {
182 return exec(file);
183 }
184
185 bool V8GL::initialize(int* pargc, char** argv, string scriptname) {
7399428 @philogb Voila
authored
186 // Create a handle scope to hold the temporary references.
187 HandleScope handle_scope;
188
189 // Create a template for the global object where we set the
190 // built-in global functions.
191 Handle<ObjectTemplate> global = ObjectTemplate::New();
192
34db969 @philogb First three examples
authored
193 // Each processor gets its own context so different processors
194 // don't affect each other.
263af37 @cscott Allow building only some of the bindings (for example on GLES-only platf...
cscott authored
195 #ifdef BUILD_GL_BINDINGS
381db7d @philogb Added method return values for the Gl object.
authored
196 Handle<ObjectTemplate> Gl = GlFactory::createGl();
263af37 @cscott Allow building only some of the bindings (for example on GLES-only platf...
cscott authored
197 #endif
198 #ifdef BUILD_GLES_BINDINGS
d833517 @philogb Changed the makefile to build the Gles object also
authored
199 Handle<ObjectTemplate> Gles = GlesFactory::createGles();
263af37 @cscott Allow building only some of the bindings (for example on GLES-only platf...
cscott authored
200 #endif
381db7d @philogb Added method return values for the Gl object.
authored
201
50431dc @philogb Added a load function to load js files. Clean up.
authored
202 //Set global objects and functions.
263af37 @cscott Allow building only some of the bindings (for example on GLES-only platf...
cscott authored
203 #ifdef BUILD_GL_BINDINGS
381db7d @philogb Added method return values for the Gl object.
authored
204 global->Set(String::New("Gl"), Gl);
263af37 @cscott Allow building only some of the bindings (for example on GLES-only platf...
cscott authored
205 #endif
206 #ifdef BUILD_GLES_BINDINGS
d833517 @philogb Changed the makefile to build the Gles object also
authored
207 global->Set(String::New("Gles"), Gles);
263af37 @cscott Allow building only some of the bindings (for example on GLES-only platf...
cscott authored
208 #endif
209 #ifdef BUILD_GLU_BINDINGS
7399428 @philogb Voila
authored
210 global->Set(String::New("Glu"), createGlu());
263af37 @cscott Allow building only some of the bindings (for example on GLES-only platf...
cscott authored
211 #endif
212 #ifdef BUILD_GLUT_BINDINGS
34db969 @philogb First three examples
authored
213 global->Set(String::New("Glut"), GlutFactory::createGlut(pargc, argv));
263af37 @cscott Allow building only some of the bindings (for example on GLES-only platf...
cscott authored
214 #endif
b773f5c @philogb added a log function callable from JS
authored
215 global->Set(String::New("log"), FunctionTemplate::New(log));
50431dc @philogb Added a load function to load js files. Clean up.
authored
216 global->Set(String::New("load"), FunctionTemplate::New(load));
7b06957 @cscott Add 'read' built-in to get file contents as a string.
cscott authored
217 global->Set(String::New("read"), FunctionTemplate::New(read));
7399428 @philogb Voila
authored
218
219 Handle<Context> context = Context::New(NULL, global);
220
50431dc @philogb Added a load function to load js files. Clean up.
authored
221 //TODO(nico): should find another way to set the right context when calling a func.
222 V8GL::context = Persistent<Context>::New(context);
263af37 @cscott Allow building only some of the bindings (for example on GLES-only platf...
cscott authored
223 #ifdef BUILD_GLUT_BINDINGS
50431dc @philogb Added a load function to load js files. Clean up.
authored
224 GlutFactory::glut_persistent_context = V8GL::context;
263af37 @cscott Allow building only some of the bindings (for example on GLES-only platf...
cscott authored
225 #endif
226 #ifdef BUILD_GLES_BINDINGS
50431dc @philogb Added a load function to load js files. Clean up.
authored
227 GlesFactory::gles_persistent_context = V8GL::context;
263af37 @cscott Allow building only some of the bindings (for example on GLES-only platf...
cscott authored
228 #endif
7399428 @philogb Voila
authored
229
230 // Enter the new context so all the following operations take place
231 // within it.
232 Context::Scope context_scope(context);
ef34aef @cscott Hook up v8-typed-array to v8-gl.
cscott authored
233 // hook up typed array support
234 v8_typed_array_init(context->Global());
7399428 @philogb Voila
authored
235
381db7d @philogb Added method return values for the Gl object.
authored
236 //Append *this* as Gl static variable so we can do dot-this-dot-that stuff
263af37 @cscott Allow building only some of the bindings (for example on GLES-only platf...
cscott authored
237 #ifdef BUILD_GL_BINDINGS
381db7d @philogb Added method return values for the Gl object.
authored
238 GlFactory::self_ = Persistent<Object>::New(Gl->NewInstance());
263af37 @cscott Allow building only some of the bindings (for example on GLES-only platf...
cscott authored
239 #endif
240 #ifdef BUILD_GLES_BINDINGS
d833517 @philogb Changed the makefile to build the Gles object also
authored
241 GlesFactory::self_ = Persistent<Object>::New(Gles->NewInstance());
263af37 @cscott Allow building only some of the bindings (for example on GLES-only platf...
cscott authored
242 #endif
381db7d @philogb Added method return values for the Gl object.
authored
243
50431dc @philogb Added a load function to load js files. Clean up.
authored
244 //Set (only once) the absolute path for the .js file being executed.
0c07674 @philogb Ambiguous Utils changed for V8GLUtils
authored
245 V8GLUtils::setRootPath(argv[0], argv[1]);
50431dc @philogb Added a load function to load js files. Clean up.
authored
246
7399428 @philogb Voila
authored
247 // Compile and run the script
50431dc @philogb Added a load function to load js files. Clean up.
authored
248 if (!executeScript(scriptname))
7399428 @philogb Voila
authored
249 return false;
250
251 return true;
252 }
Something went wrong with that request. Please try again.