2020-11-13 05:18:22 -08:00
|
|
|
/* Copyright (C) 2020 Wildfire Games.
|
2010-04-14 11:57:50 -07:00
|
|
|
* This file is part of 0 A.D.
|
|
|
|
|
*
|
|
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "lib/self_test.h"
|
|
|
|
|
|
|
|
|
|
#include "scriptinterface/ScriptInterface.h"
|
|
|
|
|
|
|
|
|
|
#include "ps/CLogger.h"
|
|
|
|
|
|
2010-05-21 17:38:33 -07:00
|
|
|
#include <boost/random/linear_congruential.hpp>
|
|
|
|
|
|
2010-04-14 11:57:50 -07:00
|
|
|
class TestScriptInterface : public CxxTest::TestSuite
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
void test_loadscript_basic()
|
|
|
|
|
{
|
2020-11-14 02:57:50 -08:00
|
|
|
ScriptInterface script("Test", "Test", g_ScriptContext);
|
2010-04-14 11:57:50 -07:00
|
|
|
TestLogger logger;
|
2012-05-04 14:48:46 -07:00
|
|
|
TS_ASSERT(script.LoadScript(L"test.js", "var x = 1+1;"));
|
2015-01-22 12:30:05 -08:00
|
|
|
TS_ASSERT_STR_NOT_CONTAINS(logger.GetOutput(), "JavaScript error");
|
|
|
|
|
TS_ASSERT_STR_NOT_CONTAINS(logger.GetOutput(), "JavaScript warning");
|
2010-04-14 11:57:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void test_loadscript_error()
|
|
|
|
|
{
|
2020-11-14 02:57:50 -08:00
|
|
|
ScriptInterface script("Test", "Test", g_ScriptContext);
|
2010-04-14 11:57:50 -07:00
|
|
|
TestLogger logger;
|
2012-05-04 14:48:46 -07:00
|
|
|
TS_ASSERT(!script.LoadScript(L"test.js", "1+"));
|
2020-11-24 07:47:03 -08:00
|
|
|
TS_ASSERT_STR_CONTAINS(logger.GetOutput(), "ERROR: JavaScript error: test.js line 3\nexpected expression, got \'}\'");
|
2010-04-14 11:57:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void test_loadscript_strict_warning()
|
|
|
|
|
{
|
2020-11-14 02:57:50 -08:00
|
|
|
ScriptInterface script("Test", "Test", g_ScriptContext);
|
2010-04-14 11:57:50 -07:00
|
|
|
TestLogger logger;
|
2014-05-08 06:26:21 -07:00
|
|
|
// in strict mode, this inside a function doesn't point to the global object
|
|
|
|
|
TS_ASSERT(script.LoadScript(L"test.js", "var isStrict = (function() { return !this; })();warn('isStrict is '+isStrict);"));
|
2015-01-22 12:30:05 -08:00
|
|
|
TS_ASSERT_STR_CONTAINS(logger.GetOutput(), "WARNING: isStrict is true");
|
2010-04-14 11:57:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void test_loadscript_strict_error()
|
|
|
|
|
{
|
2020-11-14 02:57:50 -08:00
|
|
|
ScriptInterface script("Test", "Test", g_ScriptContext);
|
2010-04-14 11:57:50 -07:00
|
|
|
TestLogger logger;
|
2012-05-04 14:48:46 -07:00
|
|
|
TS_ASSERT(!script.LoadScript(L"test.js", "with(1){}"));
|
2020-11-24 07:47:03 -08:00
|
|
|
TS_ASSERT_STR_CONTAINS(logger.GetOutput(), "ERROR: JavaScript error: test.js line 2\nstrict mode code may not contain \'with\' statements");
|
2010-04-14 11:57:50 -07:00
|
|
|
}
|
2010-05-05 15:36:35 -07:00
|
|
|
|
|
|
|
|
void test_clone_basic()
|
|
|
|
|
{
|
2020-11-14 02:57:50 -08:00
|
|
|
ScriptInterface script1("Test", "Test", g_ScriptContext);
|
|
|
|
|
ScriptInterface script2("Test", "Test", g_ScriptContext);
|
2010-05-05 15:36:35 -07:00
|
|
|
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
|
|
|
ScriptRequest rq1(script1);
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedValue obj1(rq1.cx);
|
2014-07-31 12:18:40 -07:00
|
|
|
TS_ASSERT(script1.Eval("({'x': 123, 'y': [1, 1.5, '2', 'test', undefined, null, true, false]})", &obj1));
|
|
|
|
|
|
|
|
|
|
{
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
|
|
|
ScriptRequest rq2(script2);
|
2016-11-23 03:18:37 -08:00
|
|
|
|
2020-11-18 06:39:04 -08:00
|
|
|
JS::RootedValue obj2(rq2.cx, script2.CloneValueFromOtherCompartment(script1, obj1, true));
|
2014-07-31 12:18:40 -07:00
|
|
|
|
|
|
|
|
std::string source;
|
|
|
|
|
TS_ASSERT(script2.CallFunction(obj2, "toSource", source));
|
|
|
|
|
TS_ASSERT_STR_EQUALS(source, "({x:123, y:[1, 1.5, \"2\", \"test\", (void 0), null, true, false]})");
|
|
|
|
|
}
|
2010-05-05 15:36:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void test_clone_getters()
|
|
|
|
|
{
|
|
|
|
|
// The tests should be run with JS_SetGCZeal so this can try to find GC bugs
|
2020-11-14 02:57:50 -08:00
|
|
|
ScriptInterface script1("Test", "Test", g_ScriptContext);
|
|
|
|
|
ScriptInterface script2("Test", "Test", g_ScriptContext);
|
2010-05-05 15:36:35 -07:00
|
|
|
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
|
|
|
ScriptRequest rq1(script1);
|
2016-11-23 03:18:37 -08:00
|
|
|
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedValue obj1(rq1.cx);
|
2014-07-31 12:18:40 -07:00
|
|
|
TS_ASSERT(script1.Eval("var s = '?'; var v = ({get x() { return 123 }, 'y': {'w':{get z() { delete v.y; delete v.n; v = null; s += s; return 4 }}}, 'n': 100}); v", &obj1));
|
|
|
|
|
|
|
|
|
|
{
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
|
|
|
ScriptRequest rq2(script2);
|
2016-11-23 03:18:37 -08:00
|
|
|
|
2020-11-18 06:39:04 -08:00
|
|
|
JS::RootedValue obj2(rq2.cx, script2.CloneValueFromOtherCompartment(script1, obj1, true));
|
2014-07-31 12:18:40 -07:00
|
|
|
|
|
|
|
|
std::string source;
|
|
|
|
|
TS_ASSERT(script2.CallFunction(obj2, "toSource", source));
|
|
|
|
|
TS_ASSERT_STR_EQUALS(source, "({x:123, y:{w:{z:4}}})");
|
|
|
|
|
}
|
2010-05-05 15:36:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void test_clone_cyclic()
|
|
|
|
|
{
|
2020-11-14 02:57:50 -08:00
|
|
|
ScriptInterface script1("Test", "Test", g_ScriptContext);
|
|
|
|
|
ScriptInterface script2("Test", "Test", g_ScriptContext);
|
2010-05-05 15:36:35 -07:00
|
|
|
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
|
|
|
ScriptRequest rq1(script1);
|
2016-11-23 03:18:37 -08:00
|
|
|
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedValue obj1(rq1.cx);
|
2014-07-31 12:18:40 -07:00
|
|
|
TS_ASSERT(script1.Eval("var x = []; x[0] = x; ({'a': x, 'b': x})", &obj1));
|
|
|
|
|
|
|
|
|
|
{
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
|
|
|
ScriptRequest rq2(script2);
|
2020-11-18 06:39:04 -08:00
|
|
|
JS::RootedValue obj2(rq2.cx, script2.CloneValueFromOtherCompartment(script1, obj1, true));
|
2014-07-31 12:18:40 -07:00
|
|
|
|
|
|
|
|
// Use JSAPI function to check if the values of the properties "a", "b" are equals a.x[0]
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedValue prop_a(rq2.cx);
|
|
|
|
|
JS::RootedValue prop_b(rq2.cx);
|
|
|
|
|
JS::RootedValue prop_x1(rq2.cx);
|
2014-07-31 12:18:40 -07:00
|
|
|
TS_ASSERT(script2.GetProperty(obj2, "a", &prop_a));
|
|
|
|
|
TS_ASSERT(script2.GetProperty(obj2, "b", &prop_b));
|
|
|
|
|
TS_ASSERT(prop_a.isObject());
|
|
|
|
|
TS_ASSERT(prop_b.isObject());
|
|
|
|
|
TS_ASSERT(script2.GetProperty(prop_a, "0", &prop_x1));
|
2020-11-18 06:39:04 -08:00
|
|
|
TS_ASSERT(prop_x1.get() == prop_a.get());
|
|
|
|
|
TS_ASSERT(prop_x1.get() == prop_b.get());
|
2014-07-31 12:18:40 -07:00
|
|
|
}
|
2010-05-05 15:36:35 -07:00
|
|
|
}
|
2016-11-23 03:18:37 -08:00
|
|
|
|
2014-07-26 13:31:29 -07:00
|
|
|
/**
|
|
|
|
|
* This test is mainly to make sure that all required template overloads get instantiated at least once so that compiler errors
|
|
|
|
|
* in these functions are revealed instantly (but it also tests the basic functionality of these functions).
|
|
|
|
|
*/
|
|
|
|
|
void test_rooted_templates()
|
|
|
|
|
{
|
2020-11-14 02:57:50 -08:00
|
|
|
ScriptInterface script("Test", "Test", g_ScriptContext);
|
2014-07-26 13:31:29 -07:00
|
|
|
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
|
|
|
ScriptRequest rq(script);
|
2016-11-23 03:18:37 -08:00
|
|
|
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedValue val(rq.cx);
|
|
|
|
|
JS::RootedValue out(rq.cx);
|
2014-07-31 09:34:15 -07:00
|
|
|
TS_ASSERT(script.Eval("({ "
|
|
|
|
|
"'0':0,"
|
|
|
|
|
"inc:function() { this[0]++; return this[0]; }, "
|
|
|
|
|
"setTo:function(nbr) { this[0] = nbr; }, "
|
|
|
|
|
"add:function(nbr) { this[0] += nbr; return this[0]; } "
|
|
|
|
|
"})"
|
|
|
|
|
, &val));
|
|
|
|
|
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedValue nbrVal(rq.cx, JS::NumberValue(3));
|
2014-07-26 13:31:29 -07:00
|
|
|
int nbr = 0;
|
2016-11-23 03:18:37 -08:00
|
|
|
|
2014-07-31 09:34:15 -07:00
|
|
|
// CallFunctionVoid JS::RootedValue& parameter overload
|
2014-07-26 13:31:29 -07:00
|
|
|
script.CallFunctionVoid(val, "setTo", nbrVal);
|
|
|
|
|
|
2014-07-31 09:34:15 -07:00
|
|
|
// CallFunction JS::RootedValue* out parameter overload
|
2014-07-26 13:31:29 -07:00
|
|
|
script.CallFunction(val, "inc", &out);
|
2016-11-23 03:18:37 -08:00
|
|
|
|
2020-11-13 05:18:22 -08:00
|
|
|
ScriptInterface::FromJSVal(rq, out, nbr);
|
2014-07-26 13:31:29 -07:00
|
|
|
TS_ASSERT_EQUALS(4, nbr);
|
2016-11-23 03:18:37 -08:00
|
|
|
|
2014-07-31 09:34:15 -07:00
|
|
|
// CallFunction const JS::RootedValue& parameter overload
|
2017-01-28 15:37:15 -08:00
|
|
|
script.CallFunction(val, "add", nbr, nbrVal);
|
2014-07-31 09:34:15 -07:00
|
|
|
TS_ASSERT_EQUALS(7, nbr);
|
2014-07-26 13:31:29 -07:00
|
|
|
|
2014-07-31 09:34:15 -07:00
|
|
|
// GetProperty JS::RootedValue* overload
|
2014-07-26 13:31:29 -07:00
|
|
|
nbr = 0;
|
2014-07-31 09:34:15 -07:00
|
|
|
script.GetProperty(val, "0", &out);
|
2020-11-13 05:18:22 -08:00
|
|
|
ScriptInterface::FromJSVal(rq, out, nbr);
|
2014-07-31 09:34:15 -07:00
|
|
|
TS_ASSERT_EQUALS(nbr, 7);
|
2014-07-26 13:31:29 -07:00
|
|
|
|
2014-07-31 09:34:15 -07:00
|
|
|
// GetPropertyInt JS::RootedValue* overload
|
2014-07-26 13:31:29 -07:00
|
|
|
nbr = 0;
|
|
|
|
|
script.GetPropertyInt(val, 0, &out);
|
2020-11-13 05:18:22 -08:00
|
|
|
ScriptInterface::FromJSVal(rq, out, nbr);
|
2014-07-31 09:34:15 -07:00
|
|
|
TS_ASSERT_EQUALS(nbr, 7);
|
2014-07-26 13:31:29 -07:00
|
|
|
|
|
|
|
|
handle_templates_test(script, val, &out, nbrVal);
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-23 17:32:42 -07:00
|
|
|
void handle_templates_test(const ScriptInterface& script, JS::HandleValue val, JS::MutableHandleValue out, JS::HandleValue nbrVal)
|
2014-07-26 13:31:29 -07:00
|
|
|
{
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
|
|
|
ScriptRequest rq(script);
|
2020-11-13 05:18:22 -08:00
|
|
|
|
2014-07-26 13:31:29 -07:00
|
|
|
int nbr = 0;
|
|
|
|
|
|
2014-07-31 09:34:15 -07:00
|
|
|
// CallFunctionVoid JS::HandleValue parameter overload
|
2014-07-26 13:31:29 -07:00
|
|
|
script.CallFunctionVoid(val, "setTo", nbrVal);
|
|
|
|
|
|
2014-07-31 09:34:15 -07:00
|
|
|
// CallFunction JS::MutableHandleValue out parameter overload
|
2014-07-26 13:31:29 -07:00
|
|
|
script.CallFunction(val, "inc", out);
|
2016-11-23 03:18:37 -08:00
|
|
|
|
2020-11-13 05:18:22 -08:00
|
|
|
ScriptInterface::FromJSVal(rq, out, nbr);
|
2014-07-26 13:31:29 -07:00
|
|
|
TS_ASSERT_EQUALS(4, nbr);
|
2016-11-23 03:18:37 -08:00
|
|
|
|
2014-07-31 09:34:15 -07:00
|
|
|
// CallFunction const JS::HandleValue& parameter overload
|
2017-01-28 15:37:15 -08:00
|
|
|
script.CallFunction(val, "add", nbr, nbrVal);
|
2014-07-31 09:34:15 -07:00
|
|
|
TS_ASSERT_EQUALS(7, nbr);
|
2014-07-26 13:31:29 -07:00
|
|
|
|
|
|
|
|
// GetProperty JS::MutableHandleValue overload
|
|
|
|
|
nbr = 0;
|
|
|
|
|
script.GetProperty(val, "0", out);
|
2020-11-13 05:18:22 -08:00
|
|
|
ScriptInterface::FromJSVal(rq, out, nbr);
|
2014-07-31 09:34:15 -07:00
|
|
|
TS_ASSERT_EQUALS(nbr, 7);
|
2014-07-26 13:31:29 -07:00
|
|
|
|
|
|
|
|
// GetPropertyInt JS::MutableHandleValue overload
|
|
|
|
|
nbr = 0;
|
|
|
|
|
script.GetPropertyInt(val, 0, out);
|
2020-11-13 05:18:22 -08:00
|
|
|
ScriptInterface::FromJSVal(rq, out, nbr);
|
2014-07-31 09:34:15 -07:00
|
|
|
TS_ASSERT_EQUALS(nbr, 7);
|
2014-07-26 13:31:29 -07:00
|
|
|
}
|
2010-05-21 17:38:33 -07:00
|
|
|
|
|
|
|
|
void test_random()
|
|
|
|
|
{
|
2020-11-14 02:57:50 -08:00
|
|
|
ScriptInterface script("Test", "Test", g_ScriptContext);
|
2010-05-21 17:38:33 -07:00
|
|
|
|
|
|
|
|
double d1, d2;
|
|
|
|
|
TS_ASSERT(script.Eval("Math.random()", d1));
|
|
|
|
|
TS_ASSERT(script.Eval("Math.random()", d2));
|
|
|
|
|
TS_ASSERT_DIFFERS(d1, d2);
|
|
|
|
|
|
|
|
|
|
boost::rand48 rng;
|
2012-07-02 19:16:45 -07:00
|
|
|
script.ReplaceNondeterministicRNG(rng);
|
2010-05-22 07:27:53 -07:00
|
|
|
rng.seed((u64)0);
|
2010-05-21 17:38:33 -07:00
|
|
|
TS_ASSERT(script.Eval("Math.random()", d1));
|
|
|
|
|
TS_ASSERT(script.Eval("Math.random()", d2));
|
|
|
|
|
TS_ASSERT_DIFFERS(d1, d2);
|
2010-05-22 07:27:53 -07:00
|
|
|
rng.seed((u64)0);
|
2010-05-21 17:38:33 -07:00
|
|
|
TS_ASSERT(script.Eval("Math.random()", d2));
|
|
|
|
|
TS_ASSERT_EQUALS(d1, d2);
|
|
|
|
|
}
|
2010-08-04 14:15:41 -07:00
|
|
|
|
|
|
|
|
void test_json()
|
|
|
|
|
{
|
2020-11-14 02:57:50 -08:00
|
|
|
ScriptInterface script("Test", "Test", g_ScriptContext);
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
|
|
|
ScriptRequest rq(script);
|
2010-08-04 14:15:41 -07:00
|
|
|
|
|
|
|
|
std::string input = "({'x':1,'z':[2,'3\\u263A\\ud800'],\"y\":true})";
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedValue val(rq.cx);
|
2014-08-02 15:21:50 -07:00
|
|
|
TS_ASSERT(script.Eval(input.c_str(), &val));
|
2010-08-04 14:15:41 -07:00
|
|
|
|
2014-08-02 15:21:50 -07:00
|
|
|
std::string stringified = script.StringifyJSON(&val);
|
2020-11-30 01:03:20 -08:00
|
|
|
TS_ASSERT_STR_EQUALS(stringified, "{\n \"x\": 1,\n \"z\": [\n 2,\n \"3\u263A\\ud800\"\n ],\n \"y\": true\n}");
|
2010-08-04 14:15:41 -07:00
|
|
|
|
2014-11-12 17:26:22 -08:00
|
|
|
TS_ASSERT(script.ParseJSON(stringified, &val));
|
2020-11-30 01:03:20 -08:00
|
|
|
TS_ASSERT_STR_EQUALS(script.ToString(&val), "({x:1, z:[2, \"3\\u263A\\uD800\"], y:true})");
|
2010-08-04 14:15:41 -07:00
|
|
|
}
|
2019-07-19 08:41:27 -07:00
|
|
|
|
|
|
|
|
// This function tests a common way to mod functions, by crating a wrapper that
|
|
|
|
|
// extends the functionality and is then assigned to the name of the function.
|
|
|
|
|
void test_function_override()
|
|
|
|
|
{
|
2020-11-14 02:57:50 -08:00
|
|
|
ScriptInterface script("Test", "Test", g_ScriptContext);
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
|
|
|
ScriptRequest rq(script);
|
2019-07-19 08:41:27 -07:00
|
|
|
|
|
|
|
|
TS_ASSERT(script.Eval(
|
|
|
|
|
"function f() { return 1; }"
|
|
|
|
|
"f = (function (originalFunction) {"
|
|
|
|
|
"return function () { return originalFunction() + 1; }"
|
|
|
|
|
"})(f);"
|
|
|
|
|
));
|
|
|
|
|
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedValue out(rq.cx);
|
2019-07-19 08:41:27 -07:00
|
|
|
TS_ASSERT(script.Eval("f()", &out));
|
|
|
|
|
|
|
|
|
|
int outNbr = 0;
|
2020-11-13 05:18:22 -08:00
|
|
|
ScriptInterface::FromJSVal(rq, out, outNbr);
|
2019-07-19 08:41:27 -07:00
|
|
|
TS_ASSERT_EQUALS(2, outNbr);
|
|
|
|
|
}
|
2010-04-14 11:57:50 -07:00
|
|
|
};
|