1 | /*
|
---|
2 | * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
|
---|
3 | *
|
---|
4 | * Redistribution and use in source and binary forms, with or without
|
---|
5 | * modification, are permitted provided that the following conditions
|
---|
6 | * are met:
|
---|
7 | * 1. Redistributions of source code must retain the above copyright
|
---|
8 | * notice, this list of conditions and the following disclaimer.
|
---|
9 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer in the
|
---|
11 | * documentation and/or other materials provided with the distribution.
|
---|
12 | *
|
---|
13 | * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
|
---|
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
---|
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
|
---|
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
---|
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
---|
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
---|
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
---|
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #include "config.h"
|
---|
27 | #include "JSBase.h"
|
---|
28 |
|
---|
29 | #include "APICast.h"
|
---|
30 | #include "completion.h"
|
---|
31 | #include "OpaqueJSString.h"
|
---|
32 | #include <kjs/ExecState.h>
|
---|
33 | #include <kjs/InitializeThreading.h>
|
---|
34 | #include <kjs/interpreter.h>
|
---|
35 | #include <kjs/JSGlobalObject.h>
|
---|
36 | #include <kjs/JSLock.h>
|
---|
37 | #include <kjs/JSObject.h>
|
---|
38 |
|
---|
39 | using namespace KJS;
|
---|
40 |
|
---|
41 | JSValueRef JSEvaluateScript(JSContextRef ctx, JSStringRef script, JSObjectRef thisObject, JSStringRef sourceURL, int startingLineNumber, JSValueRef* exception)
|
---|
42 | {
|
---|
43 | ExecState* exec = toJS(ctx);
|
---|
44 | exec->globalData().heap->registerThread();
|
---|
45 | JSLock lock(exec);
|
---|
46 |
|
---|
47 | JSObject* jsThisObject = toJS(thisObject);
|
---|
48 |
|
---|
49 | // Interpreter::evaluate sets "this" to the global object if it is NULL
|
---|
50 | JSGlobalObject* globalObject = exec->dynamicGlobalObject();
|
---|
51 | Completion completion = Interpreter::evaluate(globalObject->globalExec(), globalObject->globalScopeChain(), sourceURL->ustring(), startingLineNumber, script->ustring(), jsThisObject);
|
---|
52 |
|
---|
53 | if (completion.complType() == Throw) {
|
---|
54 | if (exception)
|
---|
55 | *exception = toRef(completion.value());
|
---|
56 | return 0;
|
---|
57 | }
|
---|
58 |
|
---|
59 | if (completion.value())
|
---|
60 | return toRef(completion.value());
|
---|
61 |
|
---|
62 | // happens, for example, when the only statement is an empty (';') statement
|
---|
63 | return toRef(jsUndefined());
|
---|
64 | }
|
---|
65 |
|
---|
66 | bool JSCheckScriptSyntax(JSContextRef ctx, JSStringRef script, JSStringRef sourceURL, int startingLineNumber, JSValueRef* exception)
|
---|
67 | {
|
---|
68 | ExecState* exec = toJS(ctx);
|
---|
69 | exec->globalData().heap->registerThread();
|
---|
70 | JSLock lock(exec);
|
---|
71 |
|
---|
72 | Completion completion = Interpreter::checkSyntax(exec->dynamicGlobalObject()->globalExec(), sourceURL->ustring(), startingLineNumber, script->ustring());
|
---|
73 | if (completion.complType() == Throw) {
|
---|
74 | if (exception)
|
---|
75 | *exception = toRef(completion.value());
|
---|
76 | return false;
|
---|
77 | }
|
---|
78 |
|
---|
79 | return true;
|
---|
80 | }
|
---|
81 |
|
---|
82 | void JSGarbageCollect(JSContextRef ctx)
|
---|
83 | {
|
---|
84 | // We used to recommend passing NULL as an argument here, which caused the only heap to be collected.
|
---|
85 | // As there is no longer a shared heap, the previously recommended usage became a no-op (but the GC
|
---|
86 | // will happen when the context group is destroyed).
|
---|
87 | // Because the function argument was originally ignored, some clients may pass their released context here,
|
---|
88 | // in which case there is a risk of crashing if another thread performs GC on the same heap in between.
|
---|
89 | if (!ctx)
|
---|
90 | return;
|
---|
91 |
|
---|
92 | ExecState* exec = toJS(ctx);
|
---|
93 | JSGlobalData& globalData = exec->globalData();
|
---|
94 | Heap* heap = globalData.heap;
|
---|
95 |
|
---|
96 | JSLock lock(globalData.isSharedInstance);
|
---|
97 |
|
---|
98 | if (!heap->isBusy())
|
---|
99 | heap->collect();
|
---|
100 |
|
---|
101 | // FIXME: Perhaps we should trigger a second mark and sweep
|
---|
102 | // once the garbage collector is done if this is called when
|
---|
103 | // the collector is busy.
|
---|
104 | }
|
---|