Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | |
michael@0 | 2 | /* |
michael@0 | 3 | * Copyright 2006 The Android Open Source Project |
michael@0 | 4 | * |
michael@0 | 5 | * Use of this source code is governed by a BSD-style license that can be |
michael@0 | 6 | * found in the LICENSE file. |
michael@0 | 7 | */ |
michael@0 | 8 | |
michael@0 | 9 | |
michael@0 | 10 | #include <jsapi.h> |
michael@0 | 11 | |
michael@0 | 12 | #include "SkJS.h" |
michael@0 | 13 | #include "SkString.h" |
michael@0 | 14 | |
michael@0 | 15 | #ifdef _WIN32_WCE |
michael@0 | 16 | extern "C" { |
michael@0 | 17 | void abort() { |
michael@0 | 18 | SkASSERT(0); |
michael@0 | 19 | } |
michael@0 | 20 | |
michael@0 | 21 | unsigned int _control87(unsigned int _new, unsigned int mask ) { |
michael@0 | 22 | SkASSERT(0); |
michael@0 | 23 | return 0; |
michael@0 | 24 | } |
michael@0 | 25 | |
michael@0 | 26 | time_t mktime(struct tm *timeptr ) { |
michael@0 | 27 | SkASSERT(0); |
michael@0 | 28 | return 0; |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | // int errno; |
michael@0 | 32 | |
michael@0 | 33 | char *strdup(const char *) { |
michael@0 | 34 | SkASSERT(0); |
michael@0 | 35 | return 0; |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | char *strerror(int errnum) { |
michael@0 | 39 | SkASSERT(0); |
michael@0 | 40 | return 0; |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | int isatty(void* fd) { |
michael@0 | 44 | SkASSERT(0); |
michael@0 | 45 | return 0; |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | int putenv(const char *envstring) { |
michael@0 | 49 | SkASSERT(0); |
michael@0 | 50 | return 0; |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | char *getenv(const char *varname) { |
michael@0 | 54 | SkASSERT(0); |
michael@0 | 55 | return 0; |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | void GetSystemTimeAsFileTime(LPFILETIME lpSystemTimeAsFileTime) { |
michael@0 | 59 | SkASSERT(0); |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | struct tm * localtime(const time_t *timer) { |
michael@0 | 63 | SkASSERT(0); |
michael@0 | 64 | return 0; |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | size_t strftime(char *strDest, size_t maxsize, const char *format, |
michael@0 | 68 | const struct tm *timeptr ) { |
michael@0 | 69 | SkASSERT(0); |
michael@0 | 70 | return 0; |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | } |
michael@0 | 74 | #endif |
michael@0 | 75 | |
michael@0 | 76 | static JSBool |
michael@0 | 77 | global_enumerate(JSContext *cx, JSObject *obj) |
michael@0 | 78 | { |
michael@0 | 79 | #ifdef LAZY_STANDARD_CLASSES |
michael@0 | 80 | return JS_EnumerateStandardClasses(cx, obj); |
michael@0 | 81 | #else |
michael@0 | 82 | return JS_TRUE; |
michael@0 | 83 | #endif |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | static JSBool |
michael@0 | 87 | global_resolve(JSContext *cx, JSObject *obj, jsval id, uintN flags, JSObject **objp) |
michael@0 | 88 | { |
michael@0 | 89 | #ifdef LAZY_STANDARD_CLASSES |
michael@0 | 90 | if ((flags & JSRESOLVE_ASSIGNING) == 0) { |
michael@0 | 91 | JSBool resolved; |
michael@0 | 92 | |
michael@0 | 93 | if (!JS_ResolveStandardClass(cx, obj, id, &resolved)) |
michael@0 | 94 | return JS_FALSE; |
michael@0 | 95 | if (resolved) { |
michael@0 | 96 | *objp = obj; |
michael@0 | 97 | return JS_TRUE; |
michael@0 | 98 | } |
michael@0 | 99 | } |
michael@0 | 100 | #endif |
michael@0 | 101 | |
michael@0 | 102 | #if defined(SHELL_HACK) && defined(DEBUG) && defined(XP_UNIX) |
michael@0 | 103 | if ((flags & (JSRESOLVE_QUALIFIED | JSRESOLVE_ASSIGNING)) == 0) { |
michael@0 | 104 | /* |
michael@0 | 105 | * Do this expensive hack only for unoptimized Unix builds, which are |
michael@0 | 106 | * not used for benchmarking. |
michael@0 | 107 | */ |
michael@0 | 108 | char *path, *comp, *full; |
michael@0 | 109 | const char *name; |
michael@0 | 110 | JSBool ok, found; |
michael@0 | 111 | JSFunction *fun; |
michael@0 | 112 | |
michael@0 | 113 | if (!JSVAL_IS_STRING(id)) |
michael@0 | 114 | return JS_TRUE; |
michael@0 | 115 | path = getenv("PATH"); |
michael@0 | 116 | if (!path) |
michael@0 | 117 | return JS_TRUE; |
michael@0 | 118 | path = JS_strdup(cx, path); |
michael@0 | 119 | if (!path) |
michael@0 | 120 | return JS_FALSE; |
michael@0 | 121 | name = JS_GetStringBytes(JSVAL_TO_STRING(id)); |
michael@0 | 122 | ok = JS_TRUE; |
michael@0 | 123 | for (comp = strtok(path, ":"); comp; comp = strtok(NULL, ":")) { |
michael@0 | 124 | if (*comp != '\0') { |
michael@0 | 125 | full = JS_smprintf("%s/%s", comp, name); |
michael@0 | 126 | if (!full) { |
michael@0 | 127 | JS_ReportOutOfMemory(cx); |
michael@0 | 128 | ok = JS_FALSE; |
michael@0 | 129 | break; |
michael@0 | 130 | } |
michael@0 | 131 | } else { |
michael@0 | 132 | full = (char *)name; |
michael@0 | 133 | } |
michael@0 | 134 | found = (access(full, X_OK) == 0); |
michael@0 | 135 | if (*comp != '\0') |
michael@0 | 136 | free(full); |
michael@0 | 137 | if (found) { |
michael@0 | 138 | fun = JS_DefineFunction(cx, obj, name, Exec, 0, JSPROP_ENUMERATE); |
michael@0 | 139 | ok = (fun != NULL); |
michael@0 | 140 | if (ok) |
michael@0 | 141 | *objp = obj; |
michael@0 | 142 | break; |
michael@0 | 143 | } |
michael@0 | 144 | } |
michael@0 | 145 | JS_free(cx, path); |
michael@0 | 146 | return ok; |
michael@0 | 147 | } |
michael@0 | 148 | #else |
michael@0 | 149 | return JS_TRUE; |
michael@0 | 150 | #endif |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | JSClass global_class = { |
michael@0 | 154 | "global", JSCLASS_NEW_RESOLVE, |
michael@0 | 155 | JS_PropertyStub, JS_PropertyStub, |
michael@0 | 156 | JS_PropertyStub, JS_PropertyStub, |
michael@0 | 157 | global_enumerate, (JSResolveOp) global_resolve, |
michael@0 | 158 | JS_ConvertStub, JS_FinalizeStub |
michael@0 | 159 | }; |
michael@0 | 160 | |
michael@0 | 161 | SkJS::SkJS(void* hwnd) : SkOSWindow(hwnd) { |
michael@0 | 162 | if ((fRuntime = JS_NewRuntime(0x100000)) == NULL) { |
michael@0 | 163 | SkASSERT(0); |
michael@0 | 164 | return; |
michael@0 | 165 | } |
michael@0 | 166 | if ((fContext = JS_NewContext(fRuntime, 0x1000)) == NULL) { |
michael@0 | 167 | SkASSERT(0); |
michael@0 | 168 | return; |
michael@0 | 169 | } |
michael@0 | 170 | ; |
michael@0 | 171 | if ((fGlobal = JS_NewObject(fContext, &global_class, NULL, NULL)) == NULL) { |
michael@0 | 172 | SkASSERT(0); |
michael@0 | 173 | return; |
michael@0 | 174 | } |
michael@0 | 175 | if (JS_InitStandardClasses(fContext, fGlobal) == NULL) { |
michael@0 | 176 | SkASSERT(0); |
michael@0 | 177 | return; |
michael@0 | 178 | } |
michael@0 | 179 | setConfig(SkBitmap::kARGB32_Config); |
michael@0 | 180 | updateSize(); |
michael@0 | 181 | setVisibleP(true); |
michael@0 | 182 | InitializeDisplayables(getBitmap(), fContext, fGlobal, NULL); |
michael@0 | 183 | } |
michael@0 | 184 | |
michael@0 | 185 | SkJS::~SkJS() { |
michael@0 | 186 | DisposeDisplayables(); |
michael@0 | 187 | JS_DestroyContext(fContext); |
michael@0 | 188 | JS_DestroyRuntime(fRuntime); |
michael@0 | 189 | JS_ShutDown(); |
michael@0 | 190 | } |
michael@0 | 191 | |
michael@0 | 192 | SkBool SkJS::EvaluateScript(const char* script, jsval* rVal) { |
michael@0 | 193 | return JS_EvaluateScript(fContext, fGlobal, script, strlen(script), |
michael@0 | 194 | "memory" /* no file name */, 0 /* no line number */, rVal); |
michael@0 | 195 | } |
michael@0 | 196 | |
michael@0 | 197 | SkBool SkJS::ValueToString(jsval value, SkString* string) { |
michael@0 | 198 | JSString* str = JS_ValueToString(fContext, value); |
michael@0 | 199 | if (str == NULL) |
michael@0 | 200 | return false; |
michael@0 | 201 | string->set(JS_GetStringBytes(str)); |
michael@0 | 202 | return true; |
michael@0 | 203 | } |
michael@0 | 204 | |
michael@0 | 205 | #ifdef SK_DEBUG |
michael@0 | 206 | void SkJS::Test(void* hwnd) { |
michael@0 | 207 | SkJS js(hwnd); |
michael@0 | 208 | jsval val; |
michael@0 | 209 | SkBool success = js.EvaluateScript("22/7", &val); |
michael@0 | 210 | SkASSERT(success); |
michael@0 | 211 | SkString string; |
michael@0 | 212 | success = js.ValueToString(val, &string); |
michael@0 | 213 | SkASSERT(success); |
michael@0 | 214 | SkASSERT(strcmp(string.c_str(), "3.142857142857143") == 0); |
michael@0 | 215 | success = js.EvaluateScript( |
michael@0 | 216 | "var rect = new rectangle();" |
michael@0 | 217 | "rect.left = 4;" |
michael@0 | 218 | "rect.top = 10;" |
michael@0 | 219 | "rect.right = 20;" |
michael@0 | 220 | "rect.bottom = 30;" |
michael@0 | 221 | "rect.width = rect.height + 20;" |
michael@0 | 222 | "rect.draw();" |
michael@0 | 223 | , &val); |
michael@0 | 224 | SkASSERT(success); |
michael@0 | 225 | success = js.ValueToString(val, &string); |
michael@0 | 226 | SkASSERT(success); |
michael@0 | 227 | } |
michael@0 | 228 | #endifASSERT(success); |