@ugo-studio/jspp 0.3.0 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +25 -25
- package/README.md +20 -12
- package/dist/cli/args.js +22 -0
- package/dist/cli/compiler.js +53 -0
- package/dist/cli/index.js +43 -107
- package/dist/cli/pch.js +71 -0
- package/dist/cli/runner.js +23 -0
- package/dist/cli/spinner.js +27 -11
- package/dist/cli/transpiler.js +20 -0
- package/dist/cli/utils.js +59 -0
- package/dist/cli/wasm.js +70 -0
- package/dist/index.js +17 -6
- package/dist/{analysis → interpreter/analysis}/scope.js +38 -3
- package/dist/{analysis → interpreter/analysis}/typeAnalyzer.js +563 -28
- package/dist/{core → interpreter/core}/codegen/class-handlers.js +1 -1
- package/dist/{core → interpreter/core}/codegen/control-flow-handlers.js +12 -11
- package/dist/{core → interpreter/core}/codegen/declaration-handlers.js +28 -9
- package/dist/{core → interpreter/core}/codegen/destructuring-handlers.js +9 -4
- package/dist/{core → interpreter/core}/codegen/expression-handlers.js +82 -88
- package/dist/{core → interpreter/core}/codegen/function-handlers.js +159 -46
- package/dist/{core → interpreter/core}/codegen/helpers.js +170 -25
- package/dist/interpreter/core/codegen/index.js +156 -0
- package/dist/{core → interpreter/core}/codegen/literal-handlers.js +9 -0
- package/dist/{core → interpreter/core}/codegen/statement-handlers.js +47 -7
- package/package.json +6 -4
- package/scripts/precompile-headers.ts +293 -50
- package/scripts/setup-compiler.ts +63 -63
- package/scripts/setup-emsdk.ts +114 -0
- package/src/prelude/any_value.cpp +888 -0
- package/src/prelude/any_value.hpp +29 -24
- package/src/prelude/{exception_helpers.hpp → exception.cpp} +53 -53
- package/src/prelude/exception.hpp +27 -27
- package/src/prelude/iterator_instantiations.hpp +10 -0
- package/src/prelude/{index.hpp → jspp.hpp} +13 -17
- package/src/prelude/library/array.cpp +191 -0
- package/src/prelude/library/array.hpp +5 -178
- package/src/prelude/library/boolean.cpp +30 -0
- package/src/prelude/library/boolean.hpp +14 -0
- package/src/prelude/library/console.cpp +125 -0
- package/src/prelude/library/console.hpp +9 -97
- package/src/prelude/library/error.cpp +100 -0
- package/src/prelude/library/error.hpp +8 -108
- package/src/prelude/library/function.cpp +69 -0
- package/src/prelude/library/function.hpp +6 -5
- package/src/prelude/library/global.cpp +98 -0
- package/src/prelude/library/global.hpp +12 -28
- package/src/prelude/library/global_usings.hpp +15 -0
- package/src/prelude/library/math.cpp +261 -0
- package/src/prelude/library/math.hpp +8 -288
- package/src/prelude/library/object.cpp +379 -0
- package/src/prelude/library/object.hpp +5 -267
- package/src/prelude/library/performance.cpp +21 -0
- package/src/prelude/library/performance.hpp +5 -20
- package/src/prelude/library/process.cpp +38 -0
- package/src/prelude/library/process.hpp +3 -31
- package/src/prelude/library/promise.cpp +131 -0
- package/src/prelude/library/promise.hpp +5 -116
- package/src/prelude/library/symbol.cpp +56 -0
- package/src/prelude/library/symbol.hpp +5 -46
- package/src/prelude/library/timer.cpp +88 -0
- package/src/prelude/library/timer.hpp +11 -87
- package/src/prelude/runtime.cpp +19 -0
- package/src/prelude/types.hpp +26 -20
- package/src/prelude/utils/access.hpp +123 -32
- package/src/prelude/utils/assignment_operators.hpp +119 -99
- package/src/prelude/utils/log_any_value/array.hpp +61 -40
- package/src/prelude/utils/log_any_value/function.hpp +39 -39
- package/src/prelude/utils/log_any_value/log_any_value.hpp +1 -1
- package/src/prelude/utils/log_any_value/object.hpp +60 -3
- package/src/prelude/utils/log_any_value/primitives.hpp +1 -1
- package/src/prelude/utils/operators.hpp +109 -94
- package/src/prelude/utils/operators_native.hpp +349 -0
- package/src/prelude/utils/well_known_symbols.hpp +24 -24
- package/src/prelude/values/array.cpp +1399 -0
- package/src/prelude/values/array.hpp +4 -0
- package/src/prelude/values/async_iterator.cpp +251 -0
- package/src/prelude/values/async_iterator.hpp +60 -32
- package/src/prelude/values/boolean.cpp +64 -0
- package/src/prelude/values/function.cpp +262 -0
- package/src/prelude/values/function.hpp +10 -30
- package/src/prelude/values/iterator.cpp +309 -0
- package/src/prelude/values/iterator.hpp +33 -64
- package/src/prelude/values/number.cpp +221 -0
- package/src/prelude/values/object.cpp +200 -0
- package/src/prelude/values/object.hpp +4 -0
- package/src/prelude/values/promise.cpp +479 -0
- package/src/prelude/values/promise.hpp +9 -2
- package/src/prelude/values/prototypes/array.hpp +46 -1348
- package/src/prelude/values/prototypes/async_iterator.hpp +19 -61
- package/src/prelude/values/prototypes/boolean.hpp +24 -0
- package/src/prelude/values/prototypes/function.hpp +7 -46
- package/src/prelude/values/prototypes/iterator.hpp +15 -191
- package/src/prelude/values/prototypes/number.hpp +30 -210
- package/src/prelude/values/prototypes/object.hpp +7 -23
- package/src/prelude/values/prototypes/promise.hpp +8 -186
- package/src/prelude/values/prototypes/string.hpp +28 -553
- package/src/prelude/values/prototypes/symbol.hpp +9 -70
- package/src/prelude/values/shape.hpp +52 -52
- package/src/prelude/values/string.cpp +485 -0
- package/src/prelude/values/symbol.cpp +89 -0
- package/src/prelude/values/symbol.hpp +101 -101
- package/dist/cli/file-utils.js +0 -20
- package/dist/cli-utils/args.js +0 -59
- package/dist/cli-utils/colors.js +0 -9
- package/dist/cli-utils/file-utils.js +0 -20
- package/dist/cli-utils/spinner.js +0 -55
- package/dist/cli.js +0 -153
- package/dist/core/codegen/index.js +0 -86
- package/src/prelude/any_value_access.hpp +0 -170
- package/src/prelude/any_value_defines.hpp +0 -190
- package/src/prelude/any_value_helpers.hpp +0 -374
- package/src/prelude/utils/operators_primitive.hpp +0 -337
- package/src/prelude/values/helpers/array.hpp +0 -199
- package/src/prelude/values/helpers/async_iterator.hpp +0 -275
- package/src/prelude/values/helpers/function.hpp +0 -109
- package/src/prelude/values/helpers/iterator.hpp +0 -145
- package/src/prelude/values/helpers/object.hpp +0 -104
- package/src/prelude/values/helpers/promise.hpp +0 -254
- package/src/prelude/values/helpers/string.hpp +0 -37
- package/src/prelude/values/helpers/symbol.hpp +0 -21
- /package/dist/{ast → interpreter/ast}/symbols.js +0 -0
- /package/dist/{ast → interpreter/ast}/types.js +0 -0
- /package/dist/{core → interpreter/core}/codegen/visitor.js +0 -0
- /package/dist/{core → interpreter/core}/constants.js +0 -0
- /package/dist/{core → interpreter/core}/error.js +0 -0
- /package/dist/{core → interpreter/core}/parser.js +0 -0
- /package/dist/{core → interpreter/core}/traverser.js +0 -0
|
@@ -3,111 +3,11 @@
|
|
|
3
3
|
#include "types.hpp"
|
|
4
4
|
#include "any_value.hpp"
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
jspp::AnyValue target = thisVal;
|
|
16
|
-
bool is_construct_call = false;
|
|
17
|
-
|
|
18
|
-
if (target.is_object())
|
|
19
|
-
{
|
|
20
|
-
auto obj = target.as_object();
|
|
21
|
-
if (!obj->proto.is_null() && !obj->proto.is_undefined() && is_strictly_equal_to_primitive(obj->proto, proto))
|
|
22
|
-
{
|
|
23
|
-
is_construct_call = true;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
if (!is_construct_call)
|
|
28
|
-
{
|
|
29
|
-
target = jspp::AnyValue::make_object_with_proto({}, proto);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
std::string name = "Error";
|
|
33
|
-
std::string message = "";
|
|
34
|
-
if (!args.empty() && !args[0].is_undefined())
|
|
35
|
-
{
|
|
36
|
-
message = args[0].to_std_string();
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
target.define_data_property("message", jspp::AnyValue::make_string(message), true, false, true);
|
|
40
|
-
target.define_data_property("name", jspp::AnyValue::make_string(name), true, false, true);
|
|
41
|
-
target.define_data_property("stack", jspp::AnyValue::make_string(name + ": " + message + "\n at <unknown>"), true, false, true);
|
|
42
|
-
|
|
43
|
-
if (args.size() > 1 && args[1].is_object())
|
|
44
|
-
{
|
|
45
|
-
jspp::AnyValue cause = args[1].get_own_property("cause");
|
|
46
|
-
if (!cause.is_undefined())
|
|
47
|
-
{
|
|
48
|
-
target.define_data_property("cause", cause, true, false, true);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
return target;
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
// Static Error.isError(val) implementation
|
|
56
|
-
inline auto isErrorFn = jspp::AnyValue::make_function([](jspp::AnyValue, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
|
|
57
|
-
{
|
|
58
|
-
if (args.empty()) return jspp::Constants::FALSE;
|
|
59
|
-
|
|
60
|
-
jspp::AnyValue val = args[0];
|
|
61
|
-
if (!val.is_object()) return jspp::Constants::FALSE;
|
|
62
|
-
|
|
63
|
-
// Check if it inherits from Error.prototype
|
|
64
|
-
// This is essentially 'instanceof Error'
|
|
65
|
-
jspp::AnyValue proto = Error.get_own_property("prototype");
|
|
66
|
-
|
|
67
|
-
// Walk prototype chain
|
|
68
|
-
if (val.is_object()) {
|
|
69
|
-
auto current = val.as_object()->proto;
|
|
70
|
-
while (!current.is_null()) {
|
|
71
|
-
if (is_strictly_equal_to_primitive(current, proto)) return jspp::Constants::TRUE;
|
|
72
|
-
if (current.is_object()) current = current.as_object()->proto;
|
|
73
|
-
else break;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
return jspp::Constants::FALSE; }, "isError");
|
|
78
|
-
|
|
79
|
-
// toString method for Error.prototype
|
|
80
|
-
inline auto errorToStringFn = jspp::AnyValue::make_function([](jspp::AnyValue thisVal, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
|
|
81
|
-
{
|
|
82
|
-
std::string name = "Error";
|
|
83
|
-
std::string msg = "";
|
|
84
|
-
|
|
85
|
-
jspp::AnyValue n = thisVal.get_own_property("name");
|
|
86
|
-
if (!n.is_undefined()) name = n.to_std_string();
|
|
87
|
-
|
|
88
|
-
jspp::AnyValue m = thisVal.get_own_property("message");
|
|
89
|
-
if (!m.is_undefined()) msg = m.to_std_string();
|
|
90
|
-
|
|
91
|
-
if (name.empty() && msg.empty()) return jspp::AnyValue::make_string("Error");
|
|
92
|
-
if (name.empty()) return jspp::AnyValue::make_string(msg);
|
|
93
|
-
if (msg.empty()) return jspp::AnyValue::make_string(name);
|
|
94
|
-
|
|
95
|
-
return jspp::AnyValue::make_string(name + ": " + msg); }, "toString");
|
|
96
|
-
|
|
97
|
-
// Initialize Error class and its prototype
|
|
98
|
-
struct ErrorInit
|
|
99
|
-
{
|
|
100
|
-
ErrorInit()
|
|
101
|
-
{
|
|
102
|
-
// Initialize Error class
|
|
103
|
-
Error = jspp::AnyValue::make_class(errorConstructor, "Error");
|
|
104
|
-
|
|
105
|
-
// Define Error.prototype.toString
|
|
106
|
-
auto proto = Error.get_own_property("prototype");
|
|
107
|
-
proto.define_data_property("toString", errorToStringFn, true, false, true);
|
|
108
|
-
proto.define_data_property(jspp::WellKnownSymbols::toStringTag->key, errorToStringFn, true, false, true);
|
|
109
|
-
|
|
110
|
-
// Define static Error.isError
|
|
111
|
-
Error.define_data_property("isError", isErrorFn, true, false, true);
|
|
112
|
-
}
|
|
113
|
-
} errorInit;
|
|
6
|
+
namespace jspp {
|
|
7
|
+
extern AnyValue Error;
|
|
8
|
+
void init_error();
|
|
9
|
+
extern AnyValue isErrorFn;
|
|
10
|
+
extern AnyValue errorToStringFn;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
using jspp::Error;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
#include "jspp.hpp"
|
|
2
|
+
#include "library/function.hpp"
|
|
3
|
+
|
|
4
|
+
namespace jspp
|
|
5
|
+
{
|
|
6
|
+
|
|
7
|
+
AnyValue Function = jspp::AnyValue::make_class([](jspp::AnyValue thisVal, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
|
|
8
|
+
{ return jspp::Constants::UNDEFINED; }, "Function");
|
|
9
|
+
|
|
10
|
+
struct FunctionInit
|
|
11
|
+
{
|
|
12
|
+
FunctionInit()
|
|
13
|
+
{
|
|
14
|
+
auto proto = Function.get_own_property("prototype");
|
|
15
|
+
|
|
16
|
+
// Function.prototype.call
|
|
17
|
+
proto.define_data_property("call", jspp::AnyValue::make_function([](jspp::AnyValue thisVal, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
|
|
18
|
+
{
|
|
19
|
+
if (!thisVal.is_function()) throw jspp::Exception::make_exception("Function.prototype.call called on non-function", "TypeError");
|
|
20
|
+
if (args.empty()) return thisVal.call(jspp::Constants::UNDEFINED, {});
|
|
21
|
+
auto receiver = args[0];
|
|
22
|
+
return thisVal.call(receiver, args.subspan(1)); }, "call"),
|
|
23
|
+
true, false, true);
|
|
24
|
+
|
|
25
|
+
// Function.prototype.apply
|
|
26
|
+
proto.define_data_property("apply", jspp::AnyValue::make_function([](jspp::AnyValue thisVal, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
|
|
27
|
+
{
|
|
28
|
+
if (!thisVal.is_function()) throw jspp::Exception::make_exception("Function.prototype.apply called on non-function", "TypeError");
|
|
29
|
+
auto receiver = args.size() > 0 ? args[0] : jspp::Constants::UNDEFINED;
|
|
30
|
+
auto applyArgs = args.size() > 1 ? args[1] : jspp::Constants::UNDEFINED;
|
|
31
|
+
|
|
32
|
+
if (applyArgs.is_null() || applyArgs.is_undefined()) return thisVal.call(receiver, {});
|
|
33
|
+
if (!applyArgs.is_array()) throw jspp::Exception::make_exception("CreateListFromArrayLike called on non-object", "TypeError");
|
|
34
|
+
|
|
35
|
+
auto arr = applyArgs.as_array();
|
|
36
|
+
std::vector<jspp::AnyValue> vec;
|
|
37
|
+
for(uint64_t i = 0; i < arr->length; ++i) vec.push_back(arr->get_property(static_cast<uint32_t>(i)));
|
|
38
|
+
return thisVal.call(receiver, vec); }, "apply"),
|
|
39
|
+
true, false, true);
|
|
40
|
+
|
|
41
|
+
// Function.prototype.bind
|
|
42
|
+
proto.define_data_property("bind", jspp::AnyValue::make_function([](jspp::AnyValue thisVal, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
|
|
43
|
+
{
|
|
44
|
+
if (!thisVal.is_function()) throw jspp::Exception::make_exception("Function.prototype.bind called on non-function", "TypeError");
|
|
45
|
+
auto receiver = args.size() > 0 ? args[0] : jspp::Constants::UNDEFINED;
|
|
46
|
+
std::vector<jspp::AnyValue> boundArgs(args.begin() + (args.empty() ? 0 : 1), args.end());
|
|
47
|
+
|
|
48
|
+
return jspp::AnyValue::make_function([thisVal, receiver, boundArgs](jspp::AnyValue, std::span<const jspp::AnyValue> callArgs) -> jspp::AnyValue {
|
|
49
|
+
std::vector<jspp::AnyValue> combinedArgs = boundArgs;
|
|
50
|
+
combinedArgs.insert(combinedArgs.end(), callArgs.begin(), callArgs.end());
|
|
51
|
+
return thisVal.call(receiver, combinedArgs);
|
|
52
|
+
}, thisVal.as_function()->name); }, "bind"),
|
|
53
|
+
true, false, true);
|
|
54
|
+
|
|
55
|
+
// Function.prototype.toString
|
|
56
|
+
auto toStringFn = jspp::FunctionPrototypes::get("toString");
|
|
57
|
+
if (toStringFn.has_value())
|
|
58
|
+
{
|
|
59
|
+
proto.define_data_property("toString", toStringFn.value(), true, false, true);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
void init_function_lib()
|
|
65
|
+
{
|
|
66
|
+
static FunctionInit functionInit;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
} // namespace jspp
|
|
@@ -3,8 +3,9 @@
|
|
|
3
3
|
#include "types.hpp"
|
|
4
4
|
#include "any_value.hpp"
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
6
|
+
namespace jspp {
|
|
7
|
+
extern AnyValue Function;
|
|
8
|
+
void init_function_lib();
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
using jspp::Function;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
#include "jspp.hpp"
|
|
2
|
+
#include "library/global.hpp"
|
|
3
|
+
|
|
4
|
+
namespace jspp {
|
|
5
|
+
jspp::AnyValue GeneratorFunction;
|
|
6
|
+
jspp::AnyValue AsyncFunction;
|
|
7
|
+
jspp::AnyValue AsyncGeneratorFunction;
|
|
8
|
+
jspp::AnyValue global;
|
|
9
|
+
|
|
10
|
+
void initialize_runtime() {
|
|
11
|
+
if (!global.is_undefined()) return;
|
|
12
|
+
|
|
13
|
+
// 1. Initialize core built-ins
|
|
14
|
+
init_symbol();
|
|
15
|
+
init_function_lib();
|
|
16
|
+
init_object();
|
|
17
|
+
init_array();
|
|
18
|
+
init_error();
|
|
19
|
+
init_promise();
|
|
20
|
+
init_math();
|
|
21
|
+
init_console();
|
|
22
|
+
init_boolean();
|
|
23
|
+
|
|
24
|
+
// 2. Initialize functions and linking
|
|
25
|
+
GeneratorFunction = jspp::AnyValue::make_generator([](jspp::AnyValue, std::vector<jspp::AnyValue>) -> jspp::JsIterator<jspp::AnyValue>
|
|
26
|
+
{ co_return jspp::Constants::UNDEFINED; })
|
|
27
|
+
.get_own_property("prototype")
|
|
28
|
+
.get_own_property("constructor");
|
|
29
|
+
AsyncFunction = jspp::AnyValue::make_async_function([](jspp::AnyValue, std::vector<jspp::AnyValue>) -> jspp::JsPromise
|
|
30
|
+
{ co_return jspp::Constants::UNDEFINED; })
|
|
31
|
+
.get_own_property("prototype")
|
|
32
|
+
.get_own_property("constructor");
|
|
33
|
+
AsyncGeneratorFunction = jspp::AnyValue::make_async_generator([](jspp::AnyValue, std::vector<jspp::AnyValue>) -> jspp::JsAsyncIterator<jspp::AnyValue>
|
|
34
|
+
{ co_return jspp::Constants::UNDEFINED; })
|
|
35
|
+
.get_own_property("prototype")
|
|
36
|
+
.get_own_property("constructor");
|
|
37
|
+
|
|
38
|
+
global = jspp::AnyValue::make_object({
|
|
39
|
+
{"Symbol", Symbol},
|
|
40
|
+
{"process", process},
|
|
41
|
+
{"Function", Function},
|
|
42
|
+
{"GeneratorFunction", GeneratorFunction},
|
|
43
|
+
{"AsyncFunction", AsyncFunction},
|
|
44
|
+
{"AsyncGeneratorFunction", AsyncGeneratorFunction},
|
|
45
|
+
{"console", jspp::console},
|
|
46
|
+
{"performance", performance},
|
|
47
|
+
{"Error", Error},
|
|
48
|
+
{"Promise", Promise},
|
|
49
|
+
{"setTimeout", setTimeout},
|
|
50
|
+
{"clearTimeout", clearTimeout},
|
|
51
|
+
{"setInterval", setInterval},
|
|
52
|
+
{"clearInterval", clearInterval},
|
|
53
|
+
{"Math", jspp::Math},
|
|
54
|
+
{"Object", jspp::Object},
|
|
55
|
+
{"Array", jspp::Array},
|
|
56
|
+
{"Boolean", jspp::Boolean},
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
auto objectProto = ::Object.get_own_property("prototype");
|
|
60
|
+
auto functionProto = ::Function.get_own_property("prototype");
|
|
61
|
+
auto arrayProto = ::Array.get_own_property("prototype");
|
|
62
|
+
|
|
63
|
+
auto toStringTagSym = jspp::AnyValue::from_symbol(jspp::WellKnownSymbols::toStringTag);
|
|
64
|
+
|
|
65
|
+
objectProto.define_data_property(toStringTagSym, jspp::AnyValue::make_string("Object"), true, false, true);
|
|
66
|
+
functionProto.define_data_property(toStringTagSym, jspp::AnyValue::make_string("Function"), true, false, true);
|
|
67
|
+
arrayProto.define_data_property(toStringTagSym, jspp::AnyValue::make_string("Array"), true, false, true);
|
|
68
|
+
|
|
69
|
+
// Important: Link prototypes to Object.prototype
|
|
70
|
+
arrayProto.set_prototype(objectProto);
|
|
71
|
+
functionProto.set_prototype(objectProto);
|
|
72
|
+
::Error.get_own_property("prototype").set_prototype(objectProto);
|
|
73
|
+
::Promise.get_own_property("prototype").set_prototype(objectProto);
|
|
74
|
+
::Symbol.get_own_property("prototype").set_prototype(objectProto);
|
|
75
|
+
|
|
76
|
+
auto generatorFunctionProto = GeneratorFunction.get_own_property("prototype");
|
|
77
|
+
generatorFunctionProto.set_prototype(functionProto);
|
|
78
|
+
generatorFunctionProto.define_data_property(toStringTagSym, jspp::AnyValue::make_string("GeneratorFunction"), true, false, true);
|
|
79
|
+
|
|
80
|
+
auto asyncFunctionProto = AsyncFunction.get_own_property("prototype");
|
|
81
|
+
asyncFunctionProto.set_prototype(functionProto);
|
|
82
|
+
asyncFunctionProto.define_data_property(toStringTagSym, jspp::AnyValue::make_string("AsyncFunction"), true, false, true);
|
|
83
|
+
|
|
84
|
+
auto asyncGeneratorFunctionProto = AsyncGeneratorFunction.get_own_property("prototype");
|
|
85
|
+
asyncGeneratorFunctionProto.set_prototype(functionProto);
|
|
86
|
+
asyncGeneratorFunctionProto.define_data_property(toStringTagSym, jspp::AnyValue::make_string("AsyncGeneratorFunction"), true, false, true);
|
|
87
|
+
|
|
88
|
+
::Object.set_prototype(functionProto);
|
|
89
|
+
::Array.set_prototype(functionProto);
|
|
90
|
+
::Function.set_prototype(functionProto);
|
|
91
|
+
GeneratorFunction.set_prototype(functionProto);
|
|
92
|
+
AsyncFunction.set_prototype(functionProto);
|
|
93
|
+
AsyncGeneratorFunction.set_prototype(functionProto);
|
|
94
|
+
::Error.set_prototype(functionProto);
|
|
95
|
+
::Promise.set_prototype(functionProto);
|
|
96
|
+
::Symbol.set_prototype(functionProto);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
@@ -10,32 +10,16 @@
|
|
|
10
10
|
#include "library/timer.hpp"
|
|
11
11
|
#include "library/math.hpp"
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
{"performance", performance},
|
|
19
|
-
{"Error", Error},
|
|
20
|
-
{"Promise", Promise},
|
|
21
|
-
{"setTimeout", setTimeout},
|
|
22
|
-
{"clearTimeout", clearTimeout},
|
|
23
|
-
{"setInterval", setInterval},
|
|
24
|
-
{"clearInterval", clearInterval},
|
|
25
|
-
{"Math", Math},
|
|
26
|
-
{"Object", Object},
|
|
27
|
-
{"Array", Array},
|
|
28
|
-
});
|
|
13
|
+
namespace jspp {
|
|
14
|
+
extern AnyValue GeneratorFunction;
|
|
15
|
+
extern AnyValue AsyncFunction;
|
|
16
|
+
extern AnyValue AsyncGeneratorFunction;
|
|
17
|
+
extern AnyValue global;
|
|
29
18
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
::Error.get_own_property("prototype").set_prototype(objectProto);
|
|
38
|
-
::Promise.get_own_property("prototype").set_prototype(objectProto);
|
|
39
|
-
::Symbol.get_own_property("prototype").set_prototype(objectProto);
|
|
40
|
-
}
|
|
41
|
-
} globalInit;
|
|
19
|
+
void initialize_runtime();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
using jspp::global;
|
|
23
|
+
using jspp::GeneratorFunction;
|
|
24
|
+
using jspp::AsyncFunction;
|
|
25
|
+
using jspp::AsyncGeneratorFunction;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
#include "library/console.hpp"
|
|
3
|
+
#include "library/math.hpp"
|
|
4
|
+
#include "library/object.hpp"
|
|
5
|
+
#include "library/array.hpp"
|
|
6
|
+
#include "library/error.hpp"
|
|
7
|
+
#include "library/promise.hpp"
|
|
8
|
+
|
|
9
|
+
// Global usings for the transpiled code
|
|
10
|
+
using jspp::console;
|
|
11
|
+
using jspp::Math;
|
|
12
|
+
using jspp::Object;
|
|
13
|
+
using jspp::Array;
|
|
14
|
+
using jspp::Error;
|
|
15
|
+
using jspp::Promise;
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
#include "jspp.hpp"
|
|
2
|
+
#include "library/math.hpp"
|
|
3
|
+
#include <numbers>
|
|
4
|
+
#include <random>
|
|
5
|
+
|
|
6
|
+
namespace jspp {
|
|
7
|
+
namespace Library {
|
|
8
|
+
static std::random_device rd;
|
|
9
|
+
static std::mt19937 gen(rd());
|
|
10
|
+
static std::uniform_real_distribution<> dis(0.0, 1.0);
|
|
11
|
+
|
|
12
|
+
double GetArgAsDouble(std::span<const jspp::AnyValue> args, size_t index) {
|
|
13
|
+
if (index >= args.size()) return std::numeric_limits<double>::quiet_NaN();
|
|
14
|
+
return Operators_Private::ToNumber(args[index]);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
jspp::AnyValue MathFunc1(std::span<const jspp::AnyValue> args, double (*func)(double)) {
|
|
18
|
+
return jspp::AnyValue::make_number(func(GetArgAsDouble(args, 0)));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
jspp::AnyValue MathFunc2(std::span<const jspp::AnyValue> args, double (*func)(double, double)) {
|
|
22
|
+
return jspp::AnyValue::make_number(func(GetArgAsDouble(args, 0), GetArgAsDouble(args, 1)));
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
jspp::AnyValue Math = jspp::AnyValue::make_object({});
|
|
27
|
+
|
|
28
|
+
struct MathInit {
|
|
29
|
+
MathInit() {
|
|
30
|
+
using namespace jspp::Library;
|
|
31
|
+
|
|
32
|
+
auto defConst = [](const std::string& key, double val) {
|
|
33
|
+
jspp::Math.define_data_property(key, AnyValue::make_number(val), false, false, false);
|
|
34
|
+
};
|
|
35
|
+
auto defMutable = [](const std::string& key, AnyValue val) {
|
|
36
|
+
jspp::Math.define_data_property(key, val, true, false, true);
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
defConst("E", std::numbers::e);
|
|
40
|
+
defConst("LN10", std::numbers::ln10);
|
|
41
|
+
defConst("LN2", std::numbers::ln2);
|
|
42
|
+
defConst("LOG10E", std::numbers::log10e);
|
|
43
|
+
defConst("LOG2E", std::numbers::log2e);
|
|
44
|
+
defConst("PI", std::numbers::pi);
|
|
45
|
+
defConst("SQRT1_2", std::numbers::sqrt2 / 2.0);
|
|
46
|
+
defConst("SQRT2", std::numbers::sqrt2);
|
|
47
|
+
|
|
48
|
+
defMutable("abs", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
49
|
+
return AnyValue::make_number(std::abs(GetArgAsDouble(args, 0)));
|
|
50
|
+
}, "abs"));
|
|
51
|
+
|
|
52
|
+
defMutable("acos", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
53
|
+
return MathFunc1(args, std::acos);
|
|
54
|
+
}, "acos"));
|
|
55
|
+
|
|
56
|
+
defMutable("acosh", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
57
|
+
return MathFunc1(args, std::acosh);
|
|
58
|
+
}, "acosh"));
|
|
59
|
+
|
|
60
|
+
defMutable("asin", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
61
|
+
return MathFunc1(args, std::asin);
|
|
62
|
+
}, "asin"));
|
|
63
|
+
|
|
64
|
+
defMutable("asinh", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
65
|
+
return MathFunc1(args, std::asinh);
|
|
66
|
+
}, "asinh"));
|
|
67
|
+
|
|
68
|
+
defMutable("atan", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
69
|
+
return MathFunc1(args, std::atan);
|
|
70
|
+
}, "atan"));
|
|
71
|
+
|
|
72
|
+
defMutable("atan2", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
73
|
+
return MathFunc2(args, std::atan2);
|
|
74
|
+
}, "atan2"));
|
|
75
|
+
|
|
76
|
+
defMutable("atanh", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
77
|
+
return MathFunc1(args, std::atanh);
|
|
78
|
+
}, "atanh"));
|
|
79
|
+
|
|
80
|
+
defMutable("cbrt", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
81
|
+
return MathFunc1(args, std::cbrt);
|
|
82
|
+
}, "cbrt"));
|
|
83
|
+
|
|
84
|
+
defMutable("ceil", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
85
|
+
return MathFunc1(args, std::ceil);
|
|
86
|
+
}, "ceil"));
|
|
87
|
+
|
|
88
|
+
defMutable("clz32", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
89
|
+
uint32_t val = Operators_Private::ToInt32(args.empty() ? Constants::UNDEFINED : args[0]);
|
|
90
|
+
if (val == 0) return AnyValue::make_number(32);
|
|
91
|
+
return AnyValue::make_number(std::countl_zero(val));
|
|
92
|
+
}, "clz32"));
|
|
93
|
+
|
|
94
|
+
defMutable("cos", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
95
|
+
return MathFunc1(args, std::cos);
|
|
96
|
+
}, "cos"));
|
|
97
|
+
|
|
98
|
+
defMutable("cosh", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
99
|
+
return MathFunc1(args, std::cosh);
|
|
100
|
+
}, "cosh"));
|
|
101
|
+
|
|
102
|
+
defMutable("exp", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
103
|
+
return MathFunc1(args, std::exp);
|
|
104
|
+
}, "exp"));
|
|
105
|
+
|
|
106
|
+
defMutable("expm1", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
107
|
+
return MathFunc1(args, std::expm1);
|
|
108
|
+
}, "expm1"));
|
|
109
|
+
|
|
110
|
+
defMutable("floor", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
111
|
+
return MathFunc1(args, std::floor);
|
|
112
|
+
}, "floor"));
|
|
113
|
+
|
|
114
|
+
defMutable("fround", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
115
|
+
double d = GetArgAsDouble(args, 0);
|
|
116
|
+
return AnyValue::make_number(static_cast<double>(static_cast<float>(d)));
|
|
117
|
+
}, "fround"));
|
|
118
|
+
|
|
119
|
+
defMutable("hypot", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
120
|
+
double result = 0;
|
|
121
|
+
for (const auto& arg : args) {
|
|
122
|
+
double val = Operators_Private::ToNumber(arg);
|
|
123
|
+
if (std::isinf(val)) return AnyValue::make_number(std::numeric_limits<double>::infinity());
|
|
124
|
+
result = std::hypot(result, val);
|
|
125
|
+
}
|
|
126
|
+
return AnyValue::make_number(result);
|
|
127
|
+
}, "hypot"));
|
|
128
|
+
|
|
129
|
+
defMutable("imul", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
130
|
+
int32_t a = Operators_Private::ToInt32(args.empty() ? Constants::UNDEFINED : args[0]);
|
|
131
|
+
int32_t b = Operators_Private::ToInt32(args.size() < 2 ? Constants::UNDEFINED : args[1]);
|
|
132
|
+
return AnyValue::make_number(a * b);
|
|
133
|
+
}, "imul"));
|
|
134
|
+
|
|
135
|
+
defMutable("log", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
136
|
+
return MathFunc1(args, std::log);
|
|
137
|
+
}, "log"));
|
|
138
|
+
|
|
139
|
+
defMutable("log10", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
140
|
+
return MathFunc1(args, std::log10);
|
|
141
|
+
}, "log10"));
|
|
142
|
+
|
|
143
|
+
defMutable("log1p", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
144
|
+
return MathFunc1(args, std::log1p);
|
|
145
|
+
}, "log1p"));
|
|
146
|
+
|
|
147
|
+
defMutable("log2", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
148
|
+
return MathFunc1(args, std::log2);
|
|
149
|
+
}, "log2"));
|
|
150
|
+
|
|
151
|
+
defMutable("max", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
152
|
+
double maxVal = -std::numeric_limits<double>::infinity();
|
|
153
|
+
for (const auto& arg : args) {
|
|
154
|
+
double val = Operators_Private::ToNumber(arg);
|
|
155
|
+
if (std::isnan(val)) return AnyValue::make_nan();
|
|
156
|
+
if (val > maxVal) maxVal = val;
|
|
157
|
+
}
|
|
158
|
+
return AnyValue::make_number(maxVal);
|
|
159
|
+
}, "max"));
|
|
160
|
+
|
|
161
|
+
defMutable("min", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
162
|
+
double minVal = std::numeric_limits<double>::infinity();
|
|
163
|
+
for (const auto& arg : args) {
|
|
164
|
+
double val = Operators_Private::ToNumber(arg);
|
|
165
|
+
if (std::isnan(val)) return AnyValue::make_nan();
|
|
166
|
+
if (val < minVal) minVal = val;
|
|
167
|
+
}
|
|
168
|
+
return AnyValue::make_number(minVal);
|
|
169
|
+
}, "min"));
|
|
170
|
+
|
|
171
|
+
defMutable("pow", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
172
|
+
return MathFunc2(args, std::pow);
|
|
173
|
+
}, "pow"));
|
|
174
|
+
|
|
175
|
+
defMutable("random", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
176
|
+
return AnyValue::make_number(dis(gen));
|
|
177
|
+
}, "random"));
|
|
178
|
+
|
|
179
|
+
defMutable("round", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
180
|
+
double d = GetArgAsDouble(args, 0);
|
|
181
|
+
if (std::isnan(d)) return AnyValue::make_nan();
|
|
182
|
+
return AnyValue::make_number(std::floor(d + 0.5));
|
|
183
|
+
}, "round"));
|
|
184
|
+
|
|
185
|
+
defMutable("sign", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
186
|
+
double d = GetArgAsDouble(args, 0);
|
|
187
|
+
if (std::isnan(d)) return AnyValue::make_nan();
|
|
188
|
+
if (d == 0) return AnyValue::make_number(d);
|
|
189
|
+
return AnyValue::make_number(d > 0 ? 1.0 : -1.0);
|
|
190
|
+
}, "sign"));
|
|
191
|
+
|
|
192
|
+
defMutable("sin", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
193
|
+
return MathFunc1(args, std::sin);
|
|
194
|
+
}, "sin"));
|
|
195
|
+
|
|
196
|
+
defMutable("sinh", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
197
|
+
return MathFunc1(args, std::sinh);
|
|
198
|
+
}, "sinh"));
|
|
199
|
+
|
|
200
|
+
defMutable("sqrt", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
201
|
+
return MathFunc1(args, std::sqrt);
|
|
202
|
+
}, "sqrt"));
|
|
203
|
+
|
|
204
|
+
defMutable("tan", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
205
|
+
return MathFunc1(args, std::tan);
|
|
206
|
+
}, "tan"));
|
|
207
|
+
|
|
208
|
+
defMutable("tanh", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
209
|
+
return MathFunc1(args, std::tanh);
|
|
210
|
+
}, "tanh"));
|
|
211
|
+
|
|
212
|
+
defMutable("trunc", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
213
|
+
return MathFunc1(args, std::trunc);
|
|
214
|
+
}, "trunc"));
|
|
215
|
+
|
|
216
|
+
// Math.f16round(x)
|
|
217
|
+
defMutable("f16round", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
218
|
+
double d = GetArgAsDouble(args, 0);
|
|
219
|
+
#if defined(__STDCPP_FLOAT16_T__)
|
|
220
|
+
return AnyValue::make_number(static_cast<double>(static_cast<std::float16_t>(d)));
|
|
221
|
+
#else
|
|
222
|
+
float f = static_cast<float>(d);
|
|
223
|
+
return AnyValue::make_number(static_cast<double>(f));
|
|
224
|
+
#endif
|
|
225
|
+
}, "f16round"));
|
|
226
|
+
// Math.sumPrecise(iterable)
|
|
227
|
+
defMutable("sumPrecise", AnyValue::make_function([](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue {
|
|
228
|
+
if (args.empty()) throw Exception::make_exception("Math.sumPrecise requires an iterable", "TypeError");
|
|
229
|
+
|
|
230
|
+
auto iterObj = jspp::Access::get_object_iterator(args[0], "iterable");
|
|
231
|
+
auto nextFunc = iterObj.get_own_property("next");
|
|
232
|
+
|
|
233
|
+
double sum = 0;
|
|
234
|
+
// Kahan summation algorithm for better precision
|
|
235
|
+
double c = 0;
|
|
236
|
+
|
|
237
|
+
while (true) {
|
|
238
|
+
auto nextRes = nextFunc.call(iterObj, std::span<const jspp::AnyValue>{}, "next");
|
|
239
|
+
if (is_truthy(nextRes.get_own_property("done"))) break;
|
|
240
|
+
|
|
241
|
+
double val = Operators_Private::ToNumber(nextRes.get_own_property("value"));
|
|
242
|
+
if (std::isnan(val)) {
|
|
243
|
+
sum = std::numeric_limits<double>::quiet_NaN();
|
|
244
|
+
break;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
double y = val - c;
|
|
248
|
+
double t = sum + y;
|
|
249
|
+
c = (t - sum) - y;
|
|
250
|
+
sum = t;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
return AnyValue::make_number(sum);
|
|
254
|
+
}, "sumPrecise"));
|
|
255
|
+
}
|
|
256
|
+
};
|
|
257
|
+
void init_math()
|
|
258
|
+
{
|
|
259
|
+
static MathInit mathInit;
|
|
260
|
+
}
|
|
261
|
+
}
|