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