// File: /workspace/github_c_makefile_star2_O2/mahrr[P]raven/src/vm.c
// Function: native_import
static bool native_import(VM *vm, Value *arguments, size_t count, Value *result) {
    MAYBE_UNUSED(count);

    Value argument = arguments[0];
    if (Is_String(argument) == false) {
        runtime_error(vm, "`import` expected string, got %s", type_repr(argument));
        return false;
    }

    // read the file
    const char *path = As_String(argument)->chars;
    char *source = NULL;
    {
        FILE *file = fopen(path, "rb");
        if (file == NULL) {
            runtime_error(vm, "`import` error reading '%s' (%s)", path, strerror(errno));
            return false;
        }

        fseek(file, 0L, SEEK_END);
        size_t size = ftell(file);
        rewind(file);

        source = (char *)malloc(size + 1);
        if (source == NULL) {
            runtime_error(vm, "`import` error reading '%s' (out of memory)", path);
            fclose(file);
            return false;
        }

        size_t bytes_read = fread(source, sizeof (char), size, file);
        if (bytes_read < size || ferror(file)) {
            runtime_error(vm, "`import` error reading '%s' (%s)", path, strerror(errno));
            fclose(file);
            free(source);
            return false;
        }
        fclose(file);
        source[size] = '\0';
    }

    // execute the source
    Value exported = Nil_Value;
    {
        VM sandbox = {0};
        sandbox.reset_on_exit = false;
        sandbox.allocator = vm->allocator;
        sandbox.path = path;
        sandbox.x = Nil_Value;

        reset_stack(&sandbox);
        table_init(&sandbox.globals);
        register_natives(&sandbox);

        // since the current context's object are not reachable to the sandbox they need
        // to be hided, so they are not freed in the `interpret` call by the GC, this is
        // achieved by marking the beginning of the current objects list to be the end of
        // the sandbox object list
        sandbox.allocator.objects_end = sandbox.allocator.objects;

        RavFunction *function = compile(&sandbox, source, path);
        if (function == NULL) {
            free(source);
            return false;
        }

        RavClosure *closure = object_closure(&sandbox.allocator, function);
        push(&sandbox, Obj_Value(closure));
        push_frame(&sandbox, closure, 0);

        // TODO: errors should dump the current context stack
        InterpretResult result = run_vm(&sandbox);
        if (result != INTERPRET_OK) {
            free(source);
            return false;
        }

        // reset allocator state to the current context
        sandbox.allocator.objects_end = NULL;
        vm->allocator = sandbox.allocator;

        // obtian the exported value from the X register
        exported = sandbox.x;

        // clear the sandbox resources
        table_free(&sandbox.globals);
    }

    *result = exported;

    free(source);
    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x40c88, %rsp          # imm = 0x40C88
movq %rdi, %rbx
movq (%rsi), %rdi
movq %rdi, %rax
shrq $0x32, %rax
cmpl $0x3fff, %eax           # imm = 0x3FFF
jb 0x4002
movabsq $0x3ffffffffffff, %rax  # imm = 0x3FFFFFFFFFFFF
andq %rdi, %rax
cmpl $0x0, (%rax)
je 0x4031
callq 0x4864
leaq 0x50e1(%rip), %rsi      # 0x90ef
xorl %ebp, %ebp
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x46b0
movl %ebp, %eax
addq $0x40c88, %rsp          # imm = 0x40C88
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rcx, %r14
movq 0x18(%rax), %r12
leaq 0x4fce(%rip), %rsi      # 0x900d
movq %r12, %rdi
callq 0x21c0
testq %rax, %rax
je 0x40e8
movq %rax, %r13
pushq $0x2
popq %rdx
movq %rax, %rdi
xorl %esi, %esi
callq 0x21a0
movq %r13, %rdi
callq 0x2150
movq %rax, %rbp
movq %r13, %rdi
callq 0x20c0
leaq 0x1(%rbp), %rdi
callq 0x2180
testq %rax, %rax
je 0x4112
movq %rax, %r15
pushq $0x1
popq %rsi
movq %rax, %rdi
movq %rbp, %rdx
movq %r13, %rcx
callq 0x2080
cmpq %rbp, %rax
jb 0x40ae
movq %r13, %rdi
callq 0x2070
testl %eax, %eax
je 0x4135
callq 0x2050
movl (%rax), %edi
callq 0x2210
leaq 0x504f(%rip), %rsi      # 0x9110
xorl %ebp, %ebp
movq %rbx, %rdi
movq %r12, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x46b0
movq %r13, %rdi
callq 0x20a0
movq %r15, %rdi
callq 0x2030
jmp 0x401d
callq 0x2050
movl (%rax), %edi
callq 0x2210
leaq 0x5015(%rip), %rsi      # 0x9110
xorl %ebp, %ebp
movq %rbx, %rdi
movq %r12, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x46b0
jmp 0x401d
leaq 0x5018(%rip), %rsi      # 0x9131
xorl %ebp, %ebp
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x46b0
movq %r13, %rdi
callq 0x20a0
jmp 0x401d
movq %r13, %rdi
callq 0x20a0
movb $0x0, (%r15,%rbp)
leaq 0x58(%rsp), %r13
movl $0x40c30, %edx          # imm = 0x40C30
movq %r13, %rdi
xorl %esi, %esi
callq 0x2100
pushq $0x9
popq %rcx
movq %rsp, %rbp
movq %rbp, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %r12, 0x48(%rbp)
movabsq $0x7ffc000000000001, %rax # imm = 0x7FFC000000000001
movq %rax, 0x50(%rbp)
movq %r13, 0x40058(%rbp)
leaq 0x40c70(%rsp), %r13
movq %r13, %rdi
callq 0x4a70
movq %rbp, %rdi
callq 0x25c4
movq 0x10(%rbp), %rax
movq %rax, 0x18(%rbp)
movq %rbp, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x5744
testq %rax, %rax
je 0x41ff
movabsq $-0x4000000000001, %rbp # imm = 0xFFFBFFFFFFFFFFFF
movq %rsp, %r12
movq %r12, %rdi
movq %rax, %rsi
callq 0x5072
incq %rbp
orq %rax, %rbp
movq 0x40058(%r12), %rcx
leaq 0x8(%rcx), %rdx
movq %rdx, 0x40058(%r12)
movq %rbp, (%rcx)
movq %r12, %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x28c6
movq %r12, %rdi
callq 0x2936
testl %eax, %eax
je 0x420e
movq %r15, %rdi
callq 0x2030
xorl %ebp, %ebp
jmp 0x401d
movq %rsp, %rax
andq $0x0, 0x18(%rax)
pushq $0x9
popq %rcx
movq %rbx, %rdi
movq %rax, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq 0x50(%rax), %rbx
movq %r13, %rdi
callq 0x4a83
movq %rbx, (%r14)
movq %r15, %rdi
callq 0x2030
movb $0x1, %bpl
jmp 0x401d