1
/* libs/graphics/xml/SkJS.cpp
3
** Copyright 2006, The Android Open Source Project
5
** Licensed under the Apache License, Version 2.0 (the "License");
6
** you may not use this file except in compliance with the License.
7
** You may obtain a copy of the License at
9
** http://www.apache.org/licenses/LICENSE-2.0
11
** Unless required by applicable law or agreed to in writing, software
12
** distributed under the License is distributed on an "AS IS" BASIS,
13
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
** See the License for the specific language governing permissions and
15
** limitations under the License.
29
unsigned int _control87(unsigned int _new, unsigned int mask ) {
34
time_t mktime(struct tm *timeptr ) {
41
char *strdup(const char *) {
46
char *strerror(int errnum) {
51
int isatty(void* fd) {
56
int putenv(const char *envstring) {
61
char *getenv(const char *varname) {
66
void GetSystemTimeAsFileTime(LPFILETIME lpSystemTimeAsFileTime) {
70
struct tm * localtime(const time_t *timer) {
75
size_t strftime(char *strDest, size_t maxsize, const char *format,
76
const struct tm *timeptr ) {
85
global_enumerate(JSContext *cx, JSObject *obj)
87
#ifdef LAZY_STANDARD_CLASSES
88
return JS_EnumerateStandardClasses(cx, obj);
95
global_resolve(JSContext *cx, JSObject *obj, jsval id, uintN flags, JSObject **objp)
97
#ifdef LAZY_STANDARD_CLASSES
98
if ((flags & JSRESOLVE_ASSIGNING) == 0) {
101
if (!JS_ResolveStandardClass(cx, obj, id, &resolved))
110
#if defined(SHELL_HACK) && defined(DEBUG) && defined(XP_UNIX)
111
if ((flags & (JSRESOLVE_QUALIFIED | JSRESOLVE_ASSIGNING)) == 0) {
113
* Do this expensive hack only for unoptimized Unix builds, which are
114
* not used for benchmarking.
116
char *path, *comp, *full;
121
if (!JSVAL_IS_STRING(id))
123
path = getenv("PATH");
126
path = JS_strdup(cx, path);
129
name = JS_GetStringBytes(JSVAL_TO_STRING(id));
131
for (comp = strtok(path, ":"); comp; comp = strtok(NULL, ":")) {
133
full = JS_smprintf("%s/%s", comp, name);
135
JS_ReportOutOfMemory(cx);
142
found = (access(full, X_OK) == 0);
146
fun = JS_DefineFunction(cx, obj, name, Exec, 0, JSPROP_ENUMERATE);
161
JSClass global_class = {
162
"global", JSCLASS_NEW_RESOLVE,
163
JS_PropertyStub, JS_PropertyStub,
164
JS_PropertyStub, JS_PropertyStub,
165
global_enumerate, (JSResolveOp) global_resolve,
166
JS_ConvertStub, JS_FinalizeStub
169
SkJS::SkJS(void* hwnd) : SkOSWindow(hwnd) {
170
if ((fRuntime = JS_NewRuntime(0x100000)) == NULL) {
174
if ((fContext = JS_NewContext(fRuntime, 0x1000)) == NULL) {
179
if ((fGlobal = JS_NewObject(fContext, &global_class, NULL, NULL)) == NULL) {
183
if (JS_InitStandardClasses(fContext, fGlobal) == NULL) {
187
setConfig(SkBitmap::kARGB32_Config);
190
InitializeDisplayables(getBitmap(), fContext, fGlobal, NULL);
194
DisposeDisplayables();
195
JS_DestroyContext(fContext);
196
JS_DestroyRuntime(fRuntime);
200
SkBool SkJS::EvaluateScript(const char* script, jsval* rVal) {
201
return JS_EvaluateScript(fContext, fGlobal, script, strlen(script),
202
"memory" /* no file name */, 0 /* no line number */, rVal);
205
SkBool SkJS::ValueToString(jsval value, SkString* string) {
206
JSString* str = JS_ValueToString(fContext, value);
209
string->set(JS_GetStringBytes(str));
214
void SkJS::Test(void* hwnd) {
217
SkBool success = js.EvaluateScript("22/7", &val);
220
success = js.ValueToString(val, &string);
222
SkASSERT(strcmp(string.c_str(), "3.142857142857143") == 0);
223
success = js.EvaluateScript(
224
"var rect = new rectangle();"
229
"rect.width = rect.height + 20;"
233
success = js.ValueToString(val, &string);
236
#endifASSERT(success);