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