1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/xml/SkJS.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,228 @@ 1.4 + 1.5 +/* 1.6 + * Copyright 2006 The Android Open Source Project 1.7 + * 1.8 + * Use of this source code is governed by a BSD-style license that can be 1.9 + * found in the LICENSE file. 1.10 + */ 1.11 + 1.12 + 1.13 +#include <jsapi.h> 1.14 + 1.15 +#include "SkJS.h" 1.16 +#include "SkString.h" 1.17 + 1.18 +#ifdef _WIN32_WCE 1.19 +extern "C" { 1.20 + void abort() { 1.21 + SkASSERT(0); 1.22 + } 1.23 + 1.24 + unsigned int _control87(unsigned int _new, unsigned int mask ) { 1.25 + SkASSERT(0); 1.26 + return 0; 1.27 + } 1.28 + 1.29 + time_t mktime(struct tm *timeptr ) { 1.30 + SkASSERT(0); 1.31 + return 0; 1.32 + } 1.33 + 1.34 +// int errno; 1.35 + 1.36 + char *strdup(const char *) { 1.37 + SkASSERT(0); 1.38 + return 0; 1.39 + } 1.40 + 1.41 + char *strerror(int errnum) { 1.42 + SkASSERT(0); 1.43 + return 0; 1.44 + } 1.45 + 1.46 + int isatty(void* fd) { 1.47 + SkASSERT(0); 1.48 + return 0; 1.49 + } 1.50 + 1.51 + int putenv(const char *envstring) { 1.52 + SkASSERT(0); 1.53 + return 0; 1.54 + } 1.55 + 1.56 + char *getenv(const char *varname) { 1.57 + SkASSERT(0); 1.58 + return 0; 1.59 + } 1.60 + 1.61 + void GetSystemTimeAsFileTime(LPFILETIME lpSystemTimeAsFileTime) { 1.62 + SkASSERT(0); 1.63 + } 1.64 + 1.65 + struct tm * localtime(const time_t *timer) { 1.66 + SkASSERT(0); 1.67 + return 0; 1.68 + } 1.69 + 1.70 + size_t strftime(char *strDest, size_t maxsize, const char *format, 1.71 + const struct tm *timeptr ) { 1.72 + SkASSERT(0); 1.73 + return 0; 1.74 + } 1.75 + 1.76 +} 1.77 +#endif 1.78 + 1.79 +static JSBool 1.80 +global_enumerate(JSContext *cx, JSObject *obj) 1.81 +{ 1.82 +#ifdef LAZY_STANDARD_CLASSES 1.83 + return JS_EnumerateStandardClasses(cx, obj); 1.84 +#else 1.85 + return JS_TRUE; 1.86 +#endif 1.87 +} 1.88 + 1.89 +static JSBool 1.90 +global_resolve(JSContext *cx, JSObject *obj, jsval id, uintN flags, JSObject **objp) 1.91 +{ 1.92 +#ifdef LAZY_STANDARD_CLASSES 1.93 + if ((flags & JSRESOLVE_ASSIGNING) == 0) { 1.94 + JSBool resolved; 1.95 + 1.96 + if (!JS_ResolveStandardClass(cx, obj, id, &resolved)) 1.97 + return JS_FALSE; 1.98 + if (resolved) { 1.99 + *objp = obj; 1.100 + return JS_TRUE; 1.101 + } 1.102 + } 1.103 +#endif 1.104 + 1.105 +#if defined(SHELL_HACK) && defined(DEBUG) && defined(XP_UNIX) 1.106 + if ((flags & (JSRESOLVE_QUALIFIED | JSRESOLVE_ASSIGNING)) == 0) { 1.107 + /* 1.108 + * Do this expensive hack only for unoptimized Unix builds, which are 1.109 + * not used for benchmarking. 1.110 + */ 1.111 + char *path, *comp, *full; 1.112 + const char *name; 1.113 + JSBool ok, found; 1.114 + JSFunction *fun; 1.115 + 1.116 + if (!JSVAL_IS_STRING(id)) 1.117 + return JS_TRUE; 1.118 + path = getenv("PATH"); 1.119 + if (!path) 1.120 + return JS_TRUE; 1.121 + path = JS_strdup(cx, path); 1.122 + if (!path) 1.123 + return JS_FALSE; 1.124 + name = JS_GetStringBytes(JSVAL_TO_STRING(id)); 1.125 + ok = JS_TRUE; 1.126 + for (comp = strtok(path, ":"); comp; comp = strtok(NULL, ":")) { 1.127 + if (*comp != '\0') { 1.128 + full = JS_smprintf("%s/%s", comp, name); 1.129 + if (!full) { 1.130 + JS_ReportOutOfMemory(cx); 1.131 + ok = JS_FALSE; 1.132 + break; 1.133 + } 1.134 + } else { 1.135 + full = (char *)name; 1.136 + } 1.137 + found = (access(full, X_OK) == 0); 1.138 + if (*comp != '\0') 1.139 + free(full); 1.140 + if (found) { 1.141 + fun = JS_DefineFunction(cx, obj, name, Exec, 0, JSPROP_ENUMERATE); 1.142 + ok = (fun != NULL); 1.143 + if (ok) 1.144 + *objp = obj; 1.145 + break; 1.146 + } 1.147 + } 1.148 + JS_free(cx, path); 1.149 + return ok; 1.150 + } 1.151 +#else 1.152 + return JS_TRUE; 1.153 +#endif 1.154 +} 1.155 + 1.156 +JSClass global_class = { 1.157 + "global", JSCLASS_NEW_RESOLVE, 1.158 + JS_PropertyStub, JS_PropertyStub, 1.159 + JS_PropertyStub, JS_PropertyStub, 1.160 + global_enumerate, (JSResolveOp) global_resolve, 1.161 + JS_ConvertStub, JS_FinalizeStub 1.162 +}; 1.163 + 1.164 +SkJS::SkJS(void* hwnd) : SkOSWindow(hwnd) { 1.165 + if ((fRuntime = JS_NewRuntime(0x100000)) == NULL) { 1.166 + SkASSERT(0); 1.167 + return; 1.168 + } 1.169 + if ((fContext = JS_NewContext(fRuntime, 0x1000)) == NULL) { 1.170 + SkASSERT(0); 1.171 + return; 1.172 + } 1.173 + ; 1.174 + if ((fGlobal = JS_NewObject(fContext, &global_class, NULL, NULL)) == NULL) { 1.175 + SkASSERT(0); 1.176 + return; 1.177 + } 1.178 + if (JS_InitStandardClasses(fContext, fGlobal) == NULL) { 1.179 + SkASSERT(0); 1.180 + return; 1.181 + } 1.182 + setConfig(SkBitmap::kARGB32_Config); 1.183 + updateSize(); 1.184 + setVisibleP(true); 1.185 + InitializeDisplayables(getBitmap(), fContext, fGlobal, NULL); 1.186 +} 1.187 + 1.188 +SkJS::~SkJS() { 1.189 + DisposeDisplayables(); 1.190 + JS_DestroyContext(fContext); 1.191 + JS_DestroyRuntime(fRuntime); 1.192 + JS_ShutDown(); 1.193 +} 1.194 + 1.195 +SkBool SkJS::EvaluateScript(const char* script, jsval* rVal) { 1.196 + return JS_EvaluateScript(fContext, fGlobal, script, strlen(script), 1.197 + "memory" /* no file name */, 0 /* no line number */, rVal); 1.198 +} 1.199 + 1.200 +SkBool SkJS::ValueToString(jsval value, SkString* string) { 1.201 + JSString* str = JS_ValueToString(fContext, value); 1.202 + if (str == NULL) 1.203 + return false; 1.204 + string->set(JS_GetStringBytes(str)); 1.205 + return true; 1.206 +} 1.207 + 1.208 +#ifdef SK_DEBUG 1.209 +void SkJS::Test(void* hwnd) { 1.210 + SkJS js(hwnd); 1.211 + jsval val; 1.212 + SkBool success = js.EvaluateScript("22/7", &val); 1.213 + SkASSERT(success); 1.214 + SkString string; 1.215 + success = js.ValueToString(val, &string); 1.216 + SkASSERT(success); 1.217 + SkASSERT(strcmp(string.c_str(), "3.142857142857143") == 0); 1.218 + success = js.EvaluateScript( 1.219 + "var rect = new rectangle();" 1.220 + "rect.left = 4;" 1.221 + "rect.top = 10;" 1.222 + "rect.right = 20;" 1.223 + "rect.bottom = 30;" 1.224 + "rect.width = rect.height + 20;" 1.225 + "rect.draw();" 1.226 + , &val); 1.227 + SkASSERT(success); 1.228 + success = js.ValueToString(val, &string); 1.229 + SkASSERT(success); 1.230 +} 1.231 +#endifASSERT(success);