FunctionOverridesTest.cpp
1 /* 2 * Copyright (C) 2016-2020 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 #include "FunctionOverridesTest.h" 28 29 #include "FunctionOverrides.h" 30 #include "InitializeThreading.h" 31 #include "JavaScript.h" 32 #include "Options.h" 33 34 using JSC::Options; 35 36 int testFunctionOverrides() 37 { 38 bool failed = false; 39 40 JSC::initialize(); 41 42 const char* oldFunctionOverrides = Options::functionOverrides(); 43 44 Options::functionOverrides() = "./testapiScripts/testapi-function-overrides.js"; 45 JSC::FunctionOverrides::reinstallOverrides(); 46 47 JSGlobalContextRef context = JSGlobalContextCreateInGroup(nullptr, nullptr); 48 49 JSObjectRef globalObject = JSContextGetGlobalObject(context); 50 ASSERT_UNUSED(globalObject, JSValueIsObject(context, globalObject)); 51 52 const char* scriptString = 53 "var str = '';" "\n" 54 "function f1() { /* Original f1 */ }" "\n" 55 "str += f1 + '\\n';" "\n" 56 "var f2 = function() {" "\n" 57 " // Original f2" "\n" 58 "}" "\n" 59 "str += f2 + '\\n';" "\n" 60 "str += (function() { /* Original f3 */ }) + '\\n';" "\n" 61 "var f4Source = '/* Original f4 */'" "\n" 62 "var f4 = new Function(f4Source);" "\n" 63 "str += f4 + '\\n';" "\n" 64 "\n" 65 "var expectedStr =" "\n" 66 "'function f1() { /* Overridden f1 */ }\\n" 67 "function () { /* Overridden f2 */ }\\n" 68 "function () { /* Overridden f3 */ }\\n" 69 "function anonymous() { /* Overridden f4 */ }\\n';" 70 "var result = (str == expectedStr);" "\n" 71 "result"; 72 73 JSStringRef script = JSStringCreateWithUTF8CString(scriptString); 74 JSValueRef exception = nullptr; 75 JSValueRef resultRef = JSEvaluateScript(context, script, nullptr, nullptr, 1, &exception); 76 JSStringRelease(script); 77 78 if (!JSValueIsBoolean(context, resultRef) || !JSValueToBoolean(context, resultRef)) 79 failed = true; 80 81 JSGlobalContextRelease(context); 82 83 JSC::Options::functionOverrides() = oldFunctionOverrides; 84 JSC::FunctionOverrides::reinstallOverrides(); 85 86 printf("%s: function override tests.\n", failed ? "FAIL" : "PASS"); 87 88 return failed; 89 }