]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (C) 2012 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. 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. | |
24 | */ | |
25 | ||
26 | #include "config.h" | |
27 | ||
28 | #include "APICast.h" | |
29 | #include "Completion.h" | |
30 | #include "Exception.h" | |
31 | #include "JSBasePrivate.h" | |
32 | #include "VM.h" | |
33 | #include "JSScriptRefPrivate.h" | |
34 | #include "OpaqueJSString.h" | |
35 | #include "JSCInlines.h" | |
36 | #include "Parser.h" | |
37 | #include "SourceCode.h" | |
38 | #include "SourceProvider.h" | |
39 | ||
40 | using namespace JSC; | |
41 | ||
42 | struct OpaqueJSScript : public SourceProvider { | |
43 | public: | |
44 | static WTF::RefPtr<OpaqueJSScript> create(VM* vm, const String& url, int startingLineNumber, const String& source) | |
45 | { | |
46 | return WTF::adoptRef(*new OpaqueJSScript(vm, url, startingLineNumber, source)); | |
47 | } | |
48 | ||
49 | virtual const String& source() const override | |
50 | { | |
51 | return m_source; | |
52 | } | |
53 | ||
54 | VM* vm() const { return m_vm; } | |
55 | ||
56 | private: | |
57 | OpaqueJSScript(VM* vm, const String& url, int startingLineNumber, const String& source) | |
58 | : SourceProvider(url, TextPosition(OrdinalNumber::fromOneBasedInt(startingLineNumber), OrdinalNumber::first())) | |
59 | , m_vm(vm) | |
60 | , m_source(source) | |
61 | { | |
62 | } | |
63 | ||
64 | virtual ~OpaqueJSScript() { } | |
65 | ||
66 | VM* m_vm; | |
67 | String m_source; | |
68 | }; | |
69 | ||
70 | static bool parseScript(VM* vm, const SourceCode& source, ParserError& error) | |
71 | { | |
72 | return !!JSC::parse<JSC::ProgramNode>( | |
73 | vm, source, 0, Identifier(), JSParserBuiltinMode::NotBuiltin, | |
74 | JSParserStrictMode::NotStrict, JSParserCodeType::Program, | |
75 | error); | |
76 | } | |
77 | ||
78 | extern "C" { | |
79 | ||
80 | JSScriptRef JSScriptCreateReferencingImmortalASCIIText(JSContextGroupRef contextGroup, JSStringRef url, int startingLineNumber, const char* source, size_t length, JSStringRef* errorMessage, int* errorLine) | |
81 | { | |
82 | VM* vm = toJS(contextGroup); | |
83 | JSLockHolder locker(vm); | |
84 | for (size_t i = 0; i < length; i++) { | |
85 | if (!isASCII(source[i])) | |
86 | return 0; | |
87 | } | |
88 | ||
89 | startingLineNumber = std::max(1, startingLineNumber); | |
90 | ||
91 | RefPtr<OpaqueJSScript> result = OpaqueJSScript::create(vm, url ? url->string() : String(), startingLineNumber, String(StringImpl::createFromLiteral(source, length))); | |
92 | ||
93 | ParserError error; | |
94 | if (!parseScript(vm, SourceCode(result), error)) { | |
95 | if (errorMessage) | |
96 | *errorMessage = OpaqueJSString::create(error.message()).leakRef(); | |
97 | if (errorLine) | |
98 | *errorLine = error.line(); | |
99 | return nullptr; | |
100 | } | |
101 | ||
102 | return result.release().leakRef(); | |
103 | } | |
104 | ||
105 | JSScriptRef JSScriptCreateFromString(JSContextGroupRef contextGroup, JSStringRef url, int startingLineNumber, JSStringRef source, JSStringRef* errorMessage, int* errorLine) | |
106 | { | |
107 | VM* vm = toJS(contextGroup); | |
108 | JSLockHolder locker(vm); | |
109 | ||
110 | startingLineNumber = std::max(1, startingLineNumber); | |
111 | ||
112 | RefPtr<OpaqueJSScript> result = OpaqueJSScript::create(vm, url ? url->string() : String(), startingLineNumber, source->string()); | |
113 | ||
114 | ParserError error; | |
115 | if (!parseScript(vm, SourceCode(result), error)) { | |
116 | if (errorMessage) | |
117 | *errorMessage = OpaqueJSString::create(error.message()).leakRef(); | |
118 | if (errorLine) | |
119 | *errorLine = error.line(); | |
120 | return nullptr; | |
121 | } | |
122 | ||
123 | return result.release().leakRef(); | |
124 | } | |
125 | ||
126 | void JSScriptRetain(JSScriptRef script) | |
127 | { | |
128 | JSLockHolder locker(script->vm()); | |
129 | script->ref(); | |
130 | } | |
131 | ||
132 | void JSScriptRelease(JSScriptRef script) | |
133 | { | |
134 | JSLockHolder locker(script->vm()); | |
135 | script->deref(); | |
136 | } | |
137 | ||
138 | JSValueRef JSScriptEvaluate(JSContextRef context, JSScriptRef script, JSValueRef thisValueRef, JSValueRef* exception) | |
139 | { | |
140 | ExecState* exec = toJS(context); | |
141 | JSLockHolder locker(exec); | |
142 | if (script->vm() != &exec->vm()) { | |
143 | RELEASE_ASSERT_NOT_REACHED(); | |
144 | return 0; | |
145 | } | |
146 | NakedPtr<Exception> internalException; | |
147 | JSValue thisValue = thisValueRef ? toJS(exec, thisValueRef) : jsUndefined(); | |
148 | JSValue result = evaluate(exec, SourceCode(script), thisValue, internalException); | |
149 | if (internalException) { | |
150 | if (exception) | |
151 | *exception = toRef(exec, internalException->value()); | |
152 | return 0; | |
153 | } | |
154 | ASSERT(result); | |
155 | return toRef(exec, result); | |
156 | } | |
157 | ||
158 | } |