From e316d53fa73509a6c647b355273b1c5be1ed5ecb Mon Sep 17 00:00:00 2001 From: Thakee Nathees Date: Tue, 14 Jun 2022 17:22:32 +0530 Subject: [PATCH] fiber ret updated before loading native modules --- src/core/vm.c | 7 +++++++ src/libs/std_math.c | 14 ++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/core/vm.c b/src/core/vm.c index f0b3be1..8b8eeaa 100644 --- a/src/core/vm.c +++ b/src/core/vm.c @@ -370,7 +370,14 @@ static Module* _importDL(PKVM* vm, String* resolved, String* name) { return NULL; } + // Since the DL library can use stack via slots api, we need to update + // ret and then restore it back. We're using offset instead of a pointer + // because the stack might be reallocated if it grows. + uintptr_t ret_offset = vm->fiber->ret - vm->fiber->stack; + vm->fiber->ret = vm->fiber->sp; PkHandle* pkhandle = vm->config.import_dl_fn(vm, handle); + vm->fiber->ret = vm->fiber->stack + ret_offset; + if (pkhandle == NULL) { VM_SET_ERROR(vm, stringFormat(vm, "Error loading module at \"@\"", resolved)); diff --git a/src/libs/std_math.c b/src/libs/std_math.c index eaf550c..5b8bc8f 100644 --- a/src/libs/std_math.c +++ b/src/libs/std_math.c @@ -169,6 +169,19 @@ DEF(stdMathArcTangent, pkSetSlotNumber(vm, 0, atan(num)); } +DEF(stdMathArcTan2, + "math.atan2(y:Number, x:Number) -> Number", + "These functions calculate the principal value of the arc tangent " + "of y / x, using the signs of the two arguments to determine the " + "quadrant of the result") { + + double y, x; + if (!pkValidateSlotNumber(vm, 1, &y)) return; + if (!pkValidateSlotNumber(vm, 2, &x)) return; + + pkSetSlotNumber(vm, 0, atan2(y, x)); +} + DEF(stdMathLog10, "math.log10(value:Number) -> Number", "Return the logarithm to base 10 of argument [value]") { @@ -226,6 +239,7 @@ void registerModuleMath(PKVM* vm) { REGISTER_FN(math, "asin", stdMathArcSine, 1); REGISTER_FN(math, "acos", stdMathArcCosine, 1); REGISTER_FN(math, "atan", stdMathArcTangent, 1); + REGISTER_FN(math, "atan2", stdMathArcTan2, 2); REGISTER_FN(math, "log10", stdMathLog10, 1); REGISTER_FN(math, "round", stdMathRound, 1); REGISTER_FN(math, "rand", stdMathRand, 0);