2 * Copyright (C) 2012 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
29 #include "Completion.h"
30 #include "Exception.h"
31 #include "JSBasePrivate.h"
33 #include "JSScriptRefPrivate.h"
34 #include "OpaqueJSString.h"
35 #include "JSCInlines.h"
37 #include "SourceCode.h"
38 #include "SourceProvider.h"
42 struct OpaqueJSScript : public SourceProvider {
44 static WTF::RefPtr<OpaqueJSScript> create(VM* vm, const String& url, int startingLineNumber, const String& source)
46 return WTF::adoptRef(*new OpaqueJSScript(vm, url, startingLineNumber, source));
49 unsigned hash() const override
51 return m_source.get().hash();
54 StringView source() const override
56 return m_source.get();
59 VM* vm() const { return m_vm; }
62 OpaqueJSScript(VM* vm, const String& url, int startingLineNumber, const String& source)
63 : SourceProvider(url, TextPosition(OrdinalNumber::fromOneBasedInt(startingLineNumber), OrdinalNumber::first()), SourceProviderSourceType::Program)
65 , m_source(source.isNull() ? *StringImpl::empty() : *source.impl())
69 virtual ~OpaqueJSScript() { }
72 Ref<StringImpl> m_source;
75 static bool parseScript(VM* vm, const SourceCode& source, ParserError& error)
77 return !!JSC::parse<JSC::ProgramNode>(
78 vm, source, Identifier(), JSParserBuiltinMode::NotBuiltin,
79 JSParserStrictMode::NotStrict, JSParserScriptMode::Classic, SourceParseMode::ProgramMode, SuperBinding::NotNeeded,
85 JSScriptRef JSScriptCreateReferencingImmortalASCIIText(JSContextGroupRef contextGroup, JSStringRef url, int startingLineNumber, const char* source, size_t length, JSStringRef* errorMessage, int* errorLine)
87 VM* vm = toJS(contextGroup);
88 JSLockHolder locker(vm);
89 for (size_t i = 0; i < length; i++) {
90 if (!isASCII(source[i]))
94 startingLineNumber = std::max(1, startingLineNumber);
96 auto result = OpaqueJSScript::create(vm, url ? url->string() : String(), startingLineNumber, String(StringImpl::createFromLiteral(source, length)));
99 if (!parseScript(vm, SourceCode(result), error)) {
101 *errorMessage = OpaqueJSString::create(error.message()).leakRef();
103 *errorLine = error.line();
107 return result.leakRef();
110 JSScriptRef JSScriptCreateFromString(JSContextGroupRef contextGroup, JSStringRef url, int startingLineNumber, JSStringRef source, JSStringRef* errorMessage, int* errorLine)
112 VM* vm = toJS(contextGroup);
113 JSLockHolder locker(vm);
115 startingLineNumber = std::max(1, startingLineNumber);
117 auto result = OpaqueJSScript::create(vm, url ? url->string() : String(), startingLineNumber, source->string());
120 if (!parseScript(vm, SourceCode(result), error)) {
122 *errorMessage = OpaqueJSString::create(error.message()).leakRef();
124 *errorLine = error.line();
128 return result.leakRef();
131 void JSScriptRetain(JSScriptRef script)
133 JSLockHolder locker(script->vm());
137 void JSScriptRelease(JSScriptRef script)
139 JSLockHolder locker(script->vm());
143 JSValueRef JSScriptEvaluate(JSContextRef context, JSScriptRef script, JSValueRef thisValueRef, JSValueRef* exception)
145 ExecState* exec = toJS(context);
146 JSLockHolder locker(exec);
147 if (script->vm() != &exec->vm()) {
148 RELEASE_ASSERT_NOT_REACHED();
151 NakedPtr<Exception> internalException;
152 JSValue thisValue = thisValueRef ? toJS(exec, thisValueRef) : jsUndefined();
153 JSValue result = evaluate(exec, SourceCode(script), thisValue, internalException);
154 if (internalException) {
156 *exception = toRef(exec, internalException->value());
160 return toRef(exec, result);