// File: /workspace/github_c_makefile_star2_O3/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 0x44ae
movabsq $0x3ffffffffffff, %rax  # imm = 0x3FFFFFFFFFFFF
andq %rdi, %rax
cmpl $0x0, (%rax)
jne 0x44ae
movq %rcx, %r14
movq 0x18(%rax), %r12
leaq 0x6d23(%rip), %rsi      # 0xb00d
movq %r12, %rdi
callq 0x21c0
testq %rax, %rax
je 0x4502
movq %rax, %r13
movq %rax, %rdi
xorl %esi, %esi
movl $0x2, %edx
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 0x4529
movq %rax, %r15
movl $0x1, %esi
movq %rax, %rdi
movq %rbp, %rdx
movq %r13, %rcx
callq 0x2080
cmpq %rbp, %rax
jb 0x44cb
movq %r13, %rdi
callq 0x2070
testl %eax, %eax
jne 0x44cb
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
movq %rsp, %rbp
movl $0x9, %ecx
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 0x4bf8
movq %rbp, %rdi
callq 0x28de
movq 0x10(%rbp), %rax
movq %rax, 0x18(%rbp)
movq %rbp, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x5a54
testq %rax, %rax
je 0x4467
movabsq $-0x4000000000001, %rbp # imm = 0xFFFBFFFFFFFFFFFF
movq %rsp, %r12
movq %r12, %rdi
movq %rax, %rsi
callq 0x5308
incq %rbp
orq %rax, %rbp
movq 0x40058(%r12), %rcx
leaq 0x8(%rcx), %rdx
movq %rdx, 0x40058(%r12)
movq %rbp, (%rcx)
movslq 0x40c68(%r12), %rdx
cmpq $0x80, %rdx
je 0x455b
leal 0x1(%rdx), %esi
movl %esi, 0x40c68(%rsp)
leaq (%rdx,%rdx,2), %rdx
movq %rax, 0x40068(%rsp,%rdx,8)
movq 0x10(%rax), %rax
movq 0x28(%rax), %rax
movq %rax, 0x40070(%rsp,%rdx,8)
movq %rcx, 0x40078(%rsp,%rdx,8)
movq %rsp, %rdi
callq 0x2c35
testl %eax, %eax
je 0x4476
movq %r15, %rdi
callq 0x2030
xorl %ebp, %ebp
jmp 0x4547
movq %rsp, %rax
movq $0x0, 0x18(%rax)
movl $0x9, %ecx
movq %rbx, %rdi
movq %rax, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq 0x50(%rax), %rbx
movq %r13, %rdi
callq 0x4c0e
movq %rbx, (%r14)
movq %r15, %rdi
callq 0x2030
movb $0x1, %bpl
jmp 0x4547
callq 0x4a12
leaq 0x6c35(%rip), %rsi      # 0xb0ef
xorl %ebp, %ebp
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x22dc
jmp 0x4547
callq 0x2050
movl (%rax), %edi
callq 0x2210
leaq 0x6c32(%rip), %rsi      # 0xb110
xorl %ebp, %ebp
movq %rbx, %rdi
movq %r12, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x22dc
movq %r13, %rdi
callq 0x20a0
movq %r15, %rdi
callq 0x2030
jmp 0x4547
callq 0x2050
movl (%rax), %edi
callq 0x2210
leaq 0x6bfb(%rip), %rsi      # 0xb110
xorl %ebp, %ebp
movq %rbx, %rdi
movq %r12, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x22dc
jmp 0x4547
leaq 0x6c01(%rip), %rsi      # 0xb131
xorl %ebp, %ebp
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x22dc
movq %r13, %rdi
callq 0x20a0
movl %ebp, %eax
addq $0x40c88, %rsp          # imm = 0x40C88
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6e57(%rip), %rsi      # 0xb3b9
movq %rsp, %rdi
xorl %eax, %eax
callq 0x22dc
jmp 0x445b