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 "JSBasePrivate.h"
32 #include "JSScriptRefPrivate.h"
33 #include "OpaqueJSString.h"
34 #include "JSCInlines.h"
36 #include "SourceCode.h"
37 #include "SourceProvider.h"
41 struct OpaqueJSScript
: public SourceProvider
{
43 static WTF::PassRefPtr
<OpaqueJSScript
> create(VM
* vm
, const String
& url
, int startingLineNumber
, const String
& source
)
45 return WTF::adoptRef(new OpaqueJSScript(vm
, url
, startingLineNumber
, source
));
48 virtual const String
& source() const override
53 VM
* vm() const { return m_vm
; }
56 OpaqueJSScript(VM
* vm
, const String
& url
, int startingLineNumber
, const String
& source
)
57 : SourceProvider(url
, TextPosition(OrdinalNumber::fromOneBasedInt(startingLineNumber
), OrdinalNumber::first()))
63 virtual ~OpaqueJSScript() { }
69 static bool parseScript(VM
* vm
, const SourceCode
& source
, ParserError
& error
)
71 return JSC::parse
<JSC::ProgramNode
>(vm
, source
, 0, Identifier(), JSParseNormal
, JSParseProgramCode
, error
);
76 JSScriptRef
JSScriptCreateReferencingImmortalASCIIText(JSContextGroupRef contextGroup
, JSStringRef url
, int startingLineNumber
, const char* source
, size_t length
, JSStringRef
* errorMessage
, int* errorLine
)
78 VM
* vm
= toJS(contextGroup
);
79 JSLockHolder
locker(vm
);
80 for (size_t i
= 0; i
< length
; i
++) {
81 if (!isASCII(source
[i
]))
85 startingLineNumber
= std::max(1, startingLineNumber
);
87 RefPtr
<OpaqueJSScript
> result
= OpaqueJSScript::create(vm
, url
->string(), startingLineNumber
, String(StringImpl::createFromLiteral(source
, length
)));
90 if (!parseScript(vm
, SourceCode(result
), error
)) {
92 *errorMessage
= OpaqueJSString::create(error
.m_message
).leakRef();
94 *errorLine
= error
.m_line
;
98 return result
.release().leakRef();
101 JSScriptRef
JSScriptCreateFromString(JSContextGroupRef contextGroup
, JSStringRef url
, int startingLineNumber
, JSStringRef source
, JSStringRef
* errorMessage
, int* errorLine
)
103 VM
* vm
= toJS(contextGroup
);
104 JSLockHolder
locker(vm
);
106 startingLineNumber
= std::max(1, startingLineNumber
);
108 RefPtr
<OpaqueJSScript
> result
= OpaqueJSScript::create(vm
, url
->string(), startingLineNumber
, source
->string());
111 if (!parseScript(vm
, SourceCode(result
), error
)) {
113 *errorMessage
= OpaqueJSString::create(error
.m_message
).leakRef();
115 *errorLine
= error
.m_line
;
119 return result
.release().leakRef();
122 void JSScriptRetain(JSScriptRef script
)
124 JSLockHolder
locker(script
->vm());
128 void JSScriptRelease(JSScriptRef script
)
130 JSLockHolder
locker(script
->vm());
134 JSValueRef
JSScriptEvaluate(JSContextRef context
, JSScriptRef script
, JSValueRef thisValueRef
, JSValueRef
* exception
)
136 ExecState
* exec
= toJS(context
);
137 JSLockHolder
locker(exec
);
138 if (script
->vm() != &exec
->vm()) {
139 RELEASE_ASSERT_NOT_REACHED();
142 JSValue internalException
;
143 JSValue thisValue
= thisValueRef
? toJS(exec
, thisValueRef
) : jsUndefined();
144 JSValue result
= evaluate(exec
, SourceCode(script
), thisValue
, &internalException
);
145 if (internalException
) {
147 *exception
= toRef(exec
, internalException
);
151 return toRef(exec
, result
);