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/v8/ScriptPreprocessor.h" 33 34 #include "bindings/v8/ScriptController.h" 35 #include "bindings/v8/ScriptSourceCode.h" 36 #include "bindings/v8/ScriptValue.h" 37 #include "bindings/v8/V8Binding.h" 38 #include "bindings/v8/V8ScriptRunner.h" 39 #include "core/frame/FrameConsole.h" 40 #include "core/frame/FrameHost.h" 41 #include "core/frame/LocalFrame.h" 42 #include "wtf/TemporaryChange.h" 43 44 namespace WebCore { 45 46 ScriptPreprocessor::ScriptPreprocessor(const ScriptSourceCode& preprocessorSourceCode, LocalFrame* frame) 47 : m_isPreprocessing(false) 48 { 49 RefPtr<DOMWrapperWorld> world = DOMWrapperWorld::ensureIsolatedWorld(ScriptPreprocessorIsolatedWorldId, DOMWrapperWorld::mainWorldExtensionGroup); 50 m_scriptState = ScriptState::from(toV8Context(frame, *world)); 51 52 v8::HandleScope handleScope(m_scriptState->isolate()); 53 ASSERT(frame); 54 v8::TryCatch tryCatch; 55 tryCatch.SetVerbose(true); 56 Vector<ScriptSourceCode> sources; 57 sources.append(preprocessorSourceCode); 58 Vector<v8::Local<v8::Value> > scriptResults; 59 frame->script().executeScriptInIsolatedWorld(ScriptPreprocessorIsolatedWorldId, sources, DOMWrapperWorld::mainWorldExtensionGroup, &scriptResults); 60 61 if (scriptResults.size() != 1) { 62 frame->console().addMessage(JSMessageSource, ErrorMessageLevel, "ScriptPreprocessor internal error, one ScriptSourceCode must give exactly one result."); 63 return; 64 } 65 66 v8::Local<v8::Value> preprocessorFunction = scriptResults[0]; 67 if (preprocessorFunction.IsEmpty() || !preprocessorFunction->IsFunction()) { 68 frame->console().addMessage(JSMessageSource, ErrorMessageLevel, "The preprocessor must compile to a function."); 69 return; 70 } 71 m_preprocessorFunction.set(m_scriptState->isolate(), v8::Handle<v8::Function>::Cast(preprocessorFunction)); 72 } 73 74 String ScriptPreprocessor::preprocessSourceCode(const String& sourceCode, const String& sourceName) 75 { 76 if (!isValid()) 77 return sourceCode; 78 79 return preprocessSourceCode(sourceCode, sourceName, v8::Undefined(m_scriptState->isolate())); 80 } 81 82 String ScriptPreprocessor::preprocessSourceCode(const String& sourceCode, const String& sourceName, const String& functionName) 83 { 84 if (!isValid()) 85 return sourceCode; 86 87 v8::Handle<v8::String> functionNameString = v8String(m_scriptState->isolate(), functionName); 88 return preprocessSourceCode(sourceCode, sourceName, functionNameString); 89 } 90 91 String ScriptPreprocessor::preprocessSourceCode(const String& sourceCode, const String& sourceName, v8::Handle<v8::Value> functionName) 92 { 93 if (!isValid()) 94 return sourceCode; 95 96 v8::Isolate* isolate = m_scriptState->isolate(); 97 ScriptState::Scope scope(m_scriptState.get()); 98 99 v8::Handle<v8::String> sourceCodeString = v8String(isolate, sourceCode); 100 v8::Handle<v8::String> sourceNameString = v8String(isolate, sourceName); 101 v8::Handle<v8::Value> argv[] = { sourceCodeString, sourceNameString, functionName}; 102 103 v8::TryCatch tryCatch; 104 tryCatch.SetVerbose(true); 105 TemporaryChange<bool> isPreprocessing(m_isPreprocessing, true); 106 v8::Handle<v8::Value> resultValue = V8ScriptRunner::callAsFunction(isolate, m_preprocessorFunction.newLocal(isolate), m_scriptState->context()->Global(), WTF_ARRAY_LENGTH(argv), argv); 107 108 if (!resultValue.IsEmpty() && resultValue->IsString()) 109 return toCoreStringWithNullCheck(resultValue.As<v8::String>()); 110 111 return sourceCode; 112 } 113 114 } // namespace WebCore 115