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