/********************************************************************** * * Project: MapServer * Purpose: V8 JavaScript Engine Support * Author: Alan Boudreault (aboudreault@mapgears.com) * ********************************************************************** * Copyright (c) 2013, Alan Boudreault, MapGears * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies of this Software or works derived from this Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. **********************************************************************/ #include "mapserver-config.h" #ifdef USE_V8_MAPSCRIPT #include "mapserver.h" #include "v8_mapscript.h" /* This file could be refactored in the future to encapsulate the global functions and internal use functions in a class. */ /* Handler for Javascript Exceptions. Not exposed to JavaScript, used internally. Most of the code from v8 shell example. */ void msV8ReportException(TryCatch* try_catch, const char *msg = "") { HandleScope handle_scope; if (!try_catch || !try_catch->HasCaught()) { msSetError(MS_V8ERR, "%s.", "msV8ReportException()", msg); return; } String::Utf8Value exception(try_catch->Exception()); const char* exception_string = *exception; Handle message = try_catch->Message(); if (message.IsEmpty()) { msSetError(MS_V8ERR, "Javascript Exception: %s.", "msV8ReportException()", exception_string); } else { String::Utf8Value filename(message->GetScriptResourceName()); const char* filename_string = *filename; int linenum = message->GetLineNumber(); msSetError(MS_V8ERR, "Javascript Exception: %s:%i: %s", "msV8ReportException()", filename_string, linenum, exception_string); String::Utf8Value sourceline(message->GetSourceLine()); const char* sourceline_string = *sourceline; msSetError(MS_V8ERR, "Exception source line: %s", "msV8ReportException()", sourceline_string); String::Utf8Value stack_trace(try_catch->StackTrace()); if (stack_trace.length() > 0) { const char* stack_trace_string = *stack_trace; msSetError(MS_V8ERR, "Exception StackTrace: %s", "msV8ReportException()", stack_trace_string); } } } /* This function load a javascript file in memory. */ static Handle msV8ReadFile(V8Context *v8context, const char *path) { FILE* file = fopen(path, "rb"); if (file == NULL) { char err[MS_MAXPATHLEN+21]; sprintf(err, "Error opening file: %s", path); msV8ReportException(NULL, err); return Handle(String::New("")); } fseek(file, 0, SEEK_END); int size = ftell(file); rewind(file); char* chars = new char[size + 1]; chars[size] = '\0'; for (int i = 0; i < size;) { int read = static_cast(fread(&chars[i], 1, size - i, file)); if (read == 0) { delete [] chars; fclose(file); msDebug("msV8ReadFile: error while reading file '%s'\n", path); return Undefined(); } i += read; } fclose(file); Handle result = String::New(chars, size); delete[] chars; return result; } /* Returns a compiled javascript script. */ static Handle