]> git.saurik.com Git - apple/javascriptcore.git/blame - API/JSBase.cpp
JavaScriptCore-7600.1.4.16.1.tar.gz
[apple/javascriptcore.git] / API / JSBase.cpp
CommitLineData
b37bf2e1 1/*
81345200 2 * Copyright (C) 2006, 2007, 2013 Apple Inc. All rights reserved.
b37bf2e1
A
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 *
81345200 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
b37bf2e1
A
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
81345200 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
b37bf2e1
A
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
9dae56ea 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
b37bf2e1
A
24 */
25
26#include "config.h"
27#include "JSBase.h"
9dae56ea 28#include "JSBasePrivate.h"
b37bf2e1
A
29
30#include "APICast.h"
93a37866
A
31#include "CallFrame.h"
32#include "Completion.h"
33#include "InitializeThreading.h"
34#include "JSGlobalObject.h"
35#include "JSLock.h"
36#include "JSObject.h"
9dae56ea 37#include "OpaqueJSString.h"
81345200 38#include "JSCInlines.h"
b5422865 39#include "SourceCode.h"
4e4e5a6f 40#include <wtf/text/StringHash.h>
b37bf2e1 41
81345200
A
42#if ENABLE(REMOTE_INSPECTOR)
43#include "JSGlobalObjectInspectorController.h"
44#endif
45
9dae56ea 46using namespace JSC;
b37bf2e1
A
47
48JSValueRef JSEvaluateScript(JSContextRef ctx, JSStringRef script, JSObjectRef thisObject, JSStringRef sourceURL, int startingLineNumber, JSValueRef* exception)
49{
93a37866
A
50 if (!ctx) {
51 ASSERT_NOT_REACHED();
52 return 0;
53 }
b37bf2e1 54 ExecState* exec = toJS(ctx);
81345200 55 JSLockHolder locker(exec);
9dae56ea 56
b37bf2e1 57 JSObject* jsThisObject = toJS(thisObject);
9dae56ea 58
81345200
A
59 startingLineNumber = std::max(1, startingLineNumber);
60
9dae56ea 61 // evaluate sets "this" to the global object if it is NULL
81345200 62 JSGlobalObject* globalObject = exec->vmEntryGlobalObject();
93a37866 63 SourceCode source = makeSource(script->string(), sourceURL->string(), TextPosition(OrdinalNumber::fromOneBasedInt(startingLineNumber), OrdinalNumber::first()));
b37bf2e1 64
6fe7ccc8 65 JSValue evaluationException;
93a37866 66 JSValue returnValue = evaluate(globalObject->globalExec(), source, jsThisObject, &evaluationException);
6fe7ccc8
A
67
68 if (evaluationException) {
b37bf2e1 69 if (exception)
6fe7ccc8 70 *exception = toRef(exec, evaluationException);
81345200
A
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
b37bf2e1
A
79 return 0;
80 }
ba379fdc 81
6fe7ccc8
A
82 if (returnValue)
83 return toRef(exec, returnValue);
84
b37bf2e1 85 // happens, for example, when the only statement is an empty (';') statement
ba379fdc 86 return toRef(exec, jsUndefined());
b37bf2e1
A
87}
88
89bool JSCheckScriptSyntax(JSContextRef ctx, JSStringRef script, JSStringRef sourceURL, int startingLineNumber, JSValueRef* exception)
90{
93a37866
A
91 if (!ctx) {
92 ASSERT_NOT_REACHED();
93 return false;
94 }
b37bf2e1 95 ExecState* exec = toJS(ctx);
81345200
A
96 JSLockHolder locker(exec);
97
98 startingLineNumber = std::max(1, startingLineNumber);
9dae56ea 99
93a37866 100 SourceCode source = makeSource(script->string(), sourceURL->string(), TextPosition(OrdinalNumber::fromOneBasedInt(startingLineNumber), OrdinalNumber::first()));
6fe7ccc8
A
101
102 JSValue syntaxException;
81345200 103 bool isValidSyntax = checkSyntax(exec->vmEntryGlobalObject()->globalExec(), source, &syntaxException);
6fe7ccc8
A
104
105 if (!isValidSyntax) {
b37bf2e1 106 if (exception)
6fe7ccc8 107 *exception = toRef(exec, syntaxException);
81345200
A
108#if ENABLE(REMOTE_INSPECTOR)
109 exec->vmEntryGlobalObject()->inspectorController().reportAPIException(exec, syntaxException);
110#endif
b37bf2e1
A
111 return false;
112 }
6fe7ccc8 113
b37bf2e1
A
114 return true;
115}
116
9dae56ea 117void JSGarbageCollect(JSContextRef ctx)
b37bf2e1 118{
9dae56ea
A
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);
81345200 128 JSLockHolder locker(exec);
9dae56ea 129
93a37866 130 exec->vm().heap.reportAbandonedObjectGraph();
b37bf2e1 131}
9dae56ea
A
132
133void JSReportExtraMemoryCost(JSContextRef ctx, size_t size)
134{
93a37866
A
135 if (!ctx) {
136 ASSERT_NOT_REACHED();
137 return;
138 }
139 ExecState* exec = toJS(ctx);
81345200 140 JSLockHolder locker(exec);
93a37866
A
141 exec->vm().heap.reportExtraMemoryCost(size);
142}
143
144extern "C" JS_EXPORT void JSSynchronousGarbageCollectForDebugging(JSContextRef);
81345200 145extern "C" JS_EXPORT void JSSynchronousEdenCollectForDebugging(JSContextRef);
93a37866
A
146
147void JSSynchronousGarbageCollectForDebugging(JSContextRef ctx)
148{
149 if (!ctx)
150 return;
151
9dae56ea 152 ExecState* exec = toJS(ctx);
81345200 153 JSLockHolder locker(exec);
93a37866 154 exec->vm().heap.collectAllGarbage();
9dae56ea 155}
6fe7ccc8 156
81345200
A
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
6fe7ccc8
A
167void JSDisableGCTimer(void)
168{
169 GCActivityCallback::s_shouldCreateGCTimer = false;
170}
93a37866 171
81345200
A
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