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 virtual const String
& source() const override
54 VM
* vm() const { return m_vm
; }
57 OpaqueJSScript(VM
* vm
, const String
& url
, int startingLineNumber
, const String
& source
)
58 : SourceProvider(url
, TextPosition(OrdinalNumber::fromOneBasedInt(startingLineNumber
), OrdinalNumber::first()))
64 virtual ~OpaqueJSScript() { }
70 static bool parseScript(VM
* vm
, const SourceCode
& source
, ParserError
& error
)
72 return !!JSC::parse
<JSC::ProgramNode
>(
73 vm
, source
, 0, Identifier(), JSParserBuiltinMode::NotBuiltin
,
74 JSParserStrictMode::NotStrict
, JSParserCodeType::Program
,
80 JSScriptRef
JSScriptCreateReferencingImmortalASCIIText(JSContextGroupRef contextGroup
, JSStringRef url
, int startingLineNumber
, const char* source
, size_t length
, JSStringRef
* errorMessage
, int* errorLine
)
82 VM
* vm
= toJS(contextGroup
);
83 JSLockHolder
locker(vm
);
84 for (size_t i
= 0; i
< length
; i
++) {
85 if (!isASCII(source
[i
]))
89 startingLineNumber
= std::max(1, startingLineNumber
);
91 RefPtr
<OpaqueJSScript
> result
= OpaqueJSScript::create(vm
, url
? url
->string() : String(), startingLineNumber
, String(StringImpl::createFromLiteral(source
, length
)));
94 if (!parseScript(vm
, SourceCode(result
), error
)) {
96 *errorMessage
= OpaqueJSString::create(error
.message()).leakRef();
98 *errorLine
= error
.line();
102 return result
.release().leakRef();
105 JSScriptRef
JSScriptCreateFromString(JSContextGroupRef contextGroup
, JSStringRef url
, int startingLineNumber
, JSStringRef source
, JSStringRef
* errorMessage
, int* errorLine
)
107 VM
* vm
= toJS(contextGroup
);
108 JSLockHolder
locker(vm
);
110 startingLineNumber
= std::max(1, startingLineNumber
);
112 RefPtr
<OpaqueJSScript
> result
= OpaqueJSScript::create(vm
, url
? url
->string() : String(), startingLineNumber
, source
->string());
115 if (!parseScript(vm
, SourceCode(result
), error
)) {
117 *errorMessage
= OpaqueJSString::create(error
.message()).leakRef();
119 *errorLine
= error
.line();
123 return result
.release().leakRef();
126 void JSScriptRetain(JSScriptRef script
)
128 JSLockHolder
locker(script
->vm());
132 void JSScriptRelease(JSScriptRef script
)
134 JSLockHolder
locker(script
->vm());
138 JSValueRef
JSScriptEvaluate(JSContextRef context
, JSScriptRef script
, JSValueRef thisValueRef
, JSValueRef
* exception
)
140 ExecState
* exec
= toJS(context
);
141 JSLockHolder
locker(exec
);
142 if (script
->vm() != &exec
->vm()) {
143 RELEASE_ASSERT_NOT_REACHED();
146 NakedPtr
<Exception
> internalException
;
147 JSValue thisValue
= thisValueRef
? toJS(exec
, thisValueRef
) : jsUndefined();
148 JSValue result
= evaluate(exec
, SourceCode(script
), thisValue
, internalException
);
149 if (internalException
) {
151 *exception
= toRef(exec
, internalException
->value());
155 return toRef(exec
, result
);