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