1/*
2 * Copyright (C) 2006, 2007, 2013 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 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 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#include "JSBasePrivate.h"
29
30#include "APICast.h"
31#include "CallFrame.h"
32#include "Completion.h"
33#include "InitializeThreading.h"
34#include "JSGlobalObject.h"
35#include "JSLock.h"
36#include "JSObject.h"
37#include "OpaqueJSString.h"
38#include "JSCInlines.h"
39#include "SourceCode.h"
40#include <wtf/text/StringHash.h>
41
42#if ENABLE(REMOTE_INSPECTOR)
43#include "JSGlobalObjectInspectorController.h"
44#endif
45
46using namespace JSC;
47
48JSValueRef JSEvaluateScript(JSContextRef ctx, JSStringRef script, JSObjectRef thisObject, JSStringRef sourceURL, int startingLineNumber, JSValueRef* exception)
49{
50    if (!ctx) {
51        ASSERT_NOT_REACHED();
52        return 0;
53    }
54    ExecState* exec = toJS(ctx);
55    JSLockHolder locker(exec);
56
57    JSObject* jsThisObject = toJS(thisObject);
58
59    startingLineNumber = std::max(1, startingLineNumber);
60
61    // evaluate sets "this" to the global object if it is NULL
62    JSGlobalObject* globalObject = exec->vmEntryGlobalObject();
63    SourceCode source = makeSource(script->string(), sourceURL->string(), TextPosition(OrdinalNumber::fromOneBasedInt(startingLineNumber), OrdinalNumber::first()));
64
65    JSValue evaluationException;
66    JSValue returnValue = evaluate(globalObject->globalExec(), source, jsThisObject, &evaluationException);
67
68    if (evaluationException) {
69        if (exception)
70            *exception = toRef(exec, evaluationException);
71#if ENABLE(REMOTE_INSPECTOR)
72        // FIXME: If we have a debugger attached we could learn about ParseError exceptions through
73        // ScriptDebugServer::sourceParsed and this path could produce a duplicate warning. The
74        // Debugger path is currently ignored by inspector.
75        // NOTE: If we don't have a debugger, this SourceCode will be forever lost to the inspector.
76        // We could stash it in the inspector in case an inspector is ever opened.
77        globalObject->inspectorController().reportAPIException(exec, evaluationException);
78#endif
79        return 0;
80    }
81
82    if (returnValue)
83        return toRef(exec, returnValue);
84
85    // happens, for example, when the only statement is an empty (';') statement
86    return toRef(exec, jsUndefined());
87}
88
89bool JSCheckScriptSyntax(JSContextRef ctx, JSStringRef script, JSStringRef sourceURL, int startingLineNumber, JSValueRef* exception)
90{
91    if (!ctx) {
92        ASSERT_NOT_REACHED();
93        return false;
94    }
95    ExecState* exec = toJS(ctx);
96    JSLockHolder locker(exec);
97
98    startingLineNumber = std::max(1, startingLineNumber);
99
100    SourceCode source = makeSource(script->string(), sourceURL->string(), TextPosition(OrdinalNumber::fromOneBasedInt(startingLineNumber), OrdinalNumber::first()));
101
102    JSValue syntaxException;
103    bool isValidSyntax = checkSyntax(exec->vmEntryGlobalObject()->globalExec(), source, &syntaxException);
104
105    if (!isValidSyntax) {
106        if (exception)
107            *exception = toRef(exec, syntaxException);
108#if ENABLE(REMOTE_INSPECTOR)
109        exec->vmEntryGlobalObject()->inspectorController().reportAPIException(exec, syntaxException);
110#endif
111        return false;
112    }
113
114    return true;
115}
116
117void JSGarbageCollect(JSContextRef ctx)
118{
119    // We used to recommend passing NULL as an argument here, which caused the only heap to be collected.
120    // As there is no longer a shared heap, the previously recommended usage became a no-op (but the GC
121    // will happen when the context group is destroyed).
122    // Because the function argument was originally ignored, some clients may pass their released context here,
123    // in which case there is a risk of crashing if another thread performs GC on the same heap in between.
124    if (!ctx)
125        return;
126
127    ExecState* exec = toJS(ctx);
128    JSLockHolder locker(exec);
129
130    exec->vm().heap.reportAbandonedObjectGraph();
131}
132
133void JSReportExtraMemoryCost(JSContextRef ctx, size_t size)
134{
135    if (!ctx) {
136        ASSERT_NOT_REACHED();
137        return;
138    }
139    ExecState* exec = toJS(ctx);
140    JSLockHolder locker(exec);
141    exec->vm().heap.reportExtraMemoryCost(size);
142}
143
144extern "C" JS_EXPORT void JSSynchronousGarbageCollectForDebugging(JSContextRef);
145extern "C" JS_EXPORT void JSSynchronousEdenCollectForDebugging(JSContextRef);
146
147void JSSynchronousGarbageCollectForDebugging(JSContextRef ctx)
148{
149    if (!ctx)
150        return;
151
152    ExecState* exec = toJS(ctx);
153    JSLockHolder locker(exec);
154    exec->vm().heap.collectAllGarbage();
155}
156
157void JSSynchronousEdenCollectForDebugging(JSContextRef ctx)
158{
159    if (!ctx)
160        return;
161
162    ExecState* exec = toJS(ctx);
163    JSLockHolder locker(exec);
164    exec->vm().heap.collect(EdenCollection);
165}
166
167void JSDisableGCTimer(void)
168{
169    GCActivityCallback::s_shouldCreateGCTimer = false;
170}
171
172#if PLATFORM(IOS)
173// FIXME: Expose symbols to tell dyld where to find JavaScriptCore on older versions of
174// iOS (< 7.0). We should remove these symbols once we no longer need to support such
175// versions of iOS. See <rdar://problem/13696872> for more details.
176JS_EXPORT extern const char iosInstallName43 __asm("$ld$install_name$os4.3$/System/Library/PrivateFrameworks/JavaScriptCore.framework/JavaScriptCore");
177JS_EXPORT extern const char iosInstallName50 __asm("$ld$install_name$os5.0$/System/Library/PrivateFrameworks/JavaScriptCore.framework/JavaScriptCore");
178JS_EXPORT extern const char iosInstallName51 __asm("$ld$install_name$os5.1$/System/Library/PrivateFrameworks/JavaScriptCore.framework/JavaScriptCore");
179JS_EXPORT extern const char iosInstallName60 __asm("$ld$install_name$os6.0$/System/Library/PrivateFrameworks/JavaScriptCore.framework/JavaScriptCore");
180JS_EXPORT extern const char iosInstallName61 __asm("$ld$install_name$os6.1$/System/Library/PrivateFrameworks/JavaScriptCore.framework/JavaScriptCore");
181
182const char iosInstallName43 = 0;
183const char iosInstallName50 = 0;
184const char iosInstallName51 = 0;
185const char iosInstallName60 = 0;
186const char iosInstallName61 = 0;
187#endif
188