diff --git a/core/desugarer.cpp b/core/desugarer.cpp index 635dd6ab..49496003 100644 --- a/core/desugarer.cpp +++ b/core/desugarer.cpp @@ -36,7 +36,7 @@ struct BuiltinDecl { std::vector params; }; -static unsigned long max_builtin = 39; +static unsigned long max_builtin = 40; BuiltinDecl jsonnet_builtin_decl(unsigned long builtin) { switch (builtin) { @@ -80,6 +80,7 @@ BuiltinDecl jsonnet_builtin_decl(unsigned long builtin) case 37: return {U"encodeUTF8", {U"str"}}; case 38: return {U"decodeUTF8", {U"arr"}}; case 39: return {U"atan2", {U"y", U"x"}}; + case 40: return {U"hypot", {U"a", U"b"}}; default: std::cerr << "INTERNAL ERROR: Unrecognized builtin function: " << builtin << std::endl; std::abort(); diff --git a/core/vm.cpp b/core/vm.cpp index 8ff6039a..41e5b758 100644 --- a/core/vm.cpp +++ b/core/vm.cpp @@ -938,6 +938,7 @@ class Interpreter { builtins["encodeUTF8"] = &Interpreter::builtinEncodeUTF8; builtins["decodeUTF8"] = &Interpreter::builtinDecodeUTF8; builtins["atan2"] = &Interpreter::builtinAtan2; + builtins["hypot"] = &Interpreter::builtinHypot; DesugaredObject *stdlib = makeStdlibAST(alloc, "__internal__"); jsonnet_static_analysis(stdlib); @@ -1107,6 +1108,13 @@ class Interpreter { return nullptr; } + const AST *builtinHypot(const LocationRange &loc, const std::vector &args) + { + validateBuiltinArgs(loc, "atan2", args, {Value::NUMBER, Value::NUMBER}); + scratch = makeNumberCheck(loc, std::hypot(args[0].v.d, args[1].v.d)); + return nullptr; + } + const AST *builtinType(const LocationRange &, const std::vector &args) { switch (args[0].t) { diff --git a/stdlib/std.jsonnet b/stdlib/std.jsonnet index c69dfc41..ec7ab8f4 100644 --- a/stdlib/std.jsonnet +++ b/stdlib/std.jsonnet @@ -267,16 +267,6 @@ limitations under the License. deg2rad(x):: x * std.pi / 180, rad2deg(x):: x * 180 / std.pi, - hypot(a, b):: - // copied from - // https://www.johndcook.com/blog/2010/06/02/whats-so-hard-about-finding-a-hypotenuse/ - local a_abs = std.abs(a); - local b_abs = std.abs(b); - local max = std.max(a_abs, b_abs); - local min = std.min(a_abs, b_abs); - local r = min / max; - max * std.sqrt(1 + r * r), - log2(x):: std.log(x) / std.log(2), log10(x):: std.log(x) / std.log(10),