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