From 0bcbbd7fa04e28e954742dd6937c8505e23c2663 Mon Sep 17 00:00:00 2001 From: jacopodl Date: Tue, 5 Mar 2024 15:13:34 +0100 Subject: [PATCH] feat: enable selecting the code optimization level in the eval function --- argon/vm/mod/builtins.cpp | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/argon/vm/mod/builtins.cpp b/argon/vm/mod/builtins.cpp index f311eddb..7bb346da 100644 --- a/argon/vm/mod/builtins.cpp +++ b/argon/vm/mod/builtins.cpp @@ -85,15 +85,27 @@ ARGON_FUNCTION(builtins_eval, eval, " - name: Input name.\n" " - module: Module context in which to evaluate the argon code.\n" " - src: Argon code.\n" + "- KWParameters:\n" + " - optim: Set optimization level (0-3).\n" "- Returns: A result object that contains the result of the evaluation.\n", - "s: name, m: module, sx: src", false, false) { + "s: name, m: module, sx: src", false, true) { ArBuffer buffer{}; - Result *result; + auto *f = argon::vm::GetFiber(); + int optim_lvl = 0; + + if (f != nullptr) + optim_lvl = f->context->global_config->optim_lvl; + + optim_lvl = (int) DictLookupInt((Dict *) kwargs, "optim", optim_lvl); + if (optim_lvl < 0 || optim_lvl > 3) { + ErrorFormat(kValueError[0], "invalid optimization level. Expected a value between 0 and 3, got: %d", optim_lvl); + return nullptr; + } if (!BufferGet(args[2], &buffer, BufferFlags::READ)) return nullptr; - argon::lang::CompilerWrapper c_wrapper; + argon::lang::CompilerWrapper c_wrapper(optim_lvl); auto *code = c_wrapper.Compile((const char *) ARGON_RAW_STRING((String *) args[0]), (const char *) buffer.buffer, @@ -101,6 +113,8 @@ ARGON_FUNCTION(builtins_eval, eval, BufferRelease(&buffer); + Result *result; + if (code == nullptr) { auto *err = argon::vm::GetLastError();