1 /*
2 * Copyright (C) 2013 Google 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 are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "config.h"
32 #include "bindings/core/v8/ScriptPreprocessor.h"
33
34 #include "bindings/core/v8/ScriptController.h"
35 #include "bindings/core/v8/ScriptSourceCode.h"
36 #include "bindings/core/v8/ScriptValue.h"
37 #include "bindings/core/v8/V8Binding.h"
38 #include "bindings/core/v8/V8ScriptRunner.h"
39 #include "core/frame/FrameConsole.h"
40 #include "core/frame/FrameHost.h"
41 #include "core/frame/LocalFrame.h"
42 #include "core/inspector/ConsoleMessage.h"
43 #include "wtf/TemporaryChange.h"
44
45 namespace blink {
46
ScriptPreprocessor(const ScriptSourceCode & preprocessorSourceCode,LocalFrame * frame)47 ScriptPreprocessor::ScriptPreprocessor(const ScriptSourceCode& preprocessorSourceCode, LocalFrame* frame)
48 : m_isPreprocessing(false)
49 {
50 RefPtr<DOMWrapperWorld> world = DOMWrapperWorld::ensureIsolatedWorld(ScriptPreprocessorIsolatedWorldId, DOMWrapperWorld::mainWorldExtensionGroup);
51 m_scriptState = ScriptState::from(toV8Context(frame, *world));
52
53 v8::HandleScope handleScope(m_scriptState->isolate());
54 ASSERT(frame);
55 v8::TryCatch tryCatch;
56 tryCatch.SetVerbose(true);
57 Vector<ScriptSourceCode> sources;
58 sources.append(preprocessorSourceCode);
59 Vector<v8::Local<v8::Value> > scriptResults;
60 frame->script().executeScriptInIsolatedWorld(ScriptPreprocessorIsolatedWorldId, sources, DOMWrapperWorld::mainWorldExtensionGroup, &scriptResults);
61
62 if (scriptResults.size() != 1) {
63 frame->console().addMessage(ConsoleMessage::create(JSMessageSource, ErrorMessageLevel, "ScriptPreprocessor internal error, one ScriptSourceCode must give exactly one result."));
64 return;
65 }
66
67 v8::Local<v8::Value> preprocessorFunction = scriptResults[0];
68 if (preprocessorFunction.IsEmpty() || !preprocessorFunction->IsFunction()) {
69 frame->console().addMessage(ConsoleMessage::create(JSMessageSource, ErrorMessageLevel, "The preprocessor must compile to a function."));
70 return;
71 }
72 m_preprocessorFunction.set(m_scriptState->isolate(), v8::Handle<v8::Function>::Cast(preprocessorFunction));
73 }
74
preprocessSourceCode(const String & sourceCode,const String & sourceName)75 String ScriptPreprocessor::preprocessSourceCode(const String& sourceCode, const String& sourceName)
76 {
77 if (!isValid())
78 return sourceCode;
79
80 return preprocessSourceCode(sourceCode, sourceName, v8::Undefined(m_scriptState->isolate()));
81 }
82
preprocessSourceCode(const String & sourceCode,const String & sourceName,const String & functionName)83 String ScriptPreprocessor::preprocessSourceCode(const String& sourceCode, const String& sourceName, const String& functionName)
84 {
85 if (!isValid())
86 return sourceCode;
87
88 v8::Handle<v8::String> functionNameString = v8String(m_scriptState->isolate(), functionName);
89 return preprocessSourceCode(sourceCode, sourceName, functionNameString);
90 }
91
preprocessSourceCode(const String & sourceCode,const String & sourceName,v8::Handle<v8::Value> functionName)92 String ScriptPreprocessor::preprocessSourceCode(const String& sourceCode, const String& sourceName, v8::Handle<v8::Value> functionName)
93 {
94 if (!isValid())
95 return sourceCode;
96
97 v8::Isolate* isolate = m_scriptState->isolate();
98 ScriptState::Scope scope(m_scriptState.get());
99
100 v8::Handle<v8::String> sourceCodeString = v8String(isolate, sourceCode);
101 v8::Handle<v8::String> sourceNameString = v8String(isolate, sourceName);
102 v8::Handle<v8::Value> argv[] = { sourceCodeString, sourceNameString, functionName};
103
104 v8::TryCatch tryCatch;
105 tryCatch.SetVerbose(true);
106 TemporaryChange<bool> isPreprocessing(m_isPreprocessing, true);
107 v8::Handle<v8::Value> resultValue = V8ScriptRunner::callAsFunction(isolate, m_preprocessorFunction.newLocal(isolate), m_scriptState->context()->Global(), WTF_ARRAY_LENGTH(argv), argv);
108
109 if (!resultValue.IsEmpty() && resultValue->IsString())
110 return toCoreStringWithNullCheck(resultValue.As<v8::String>());
111
112 return sourceCode;
113 }
114
115 } // namespace blink
116