Skip to content

How to turn on JIT mode when using the C interface? #3885

Description

@csbinghu

Downloaded the daslang-bundle-windows-x86_64.zip file for daslang version 0.6.3 preop. A C++ application uses daslang in an embedded way. Calling daslang's C interface can successfully run programs written in the daslang language. However, after adding the [jit] attribute to a function, calling the jit_enabled() function returns false. The specific code is as follows: [code not provided in the original text]. How can JIT be enabled?

int main() {
// 1. 初始化 Daslang 引擎
das_initialize();

// 2. 创建必要的辅助对象
das_text_writer *tout = das_text_make_printer();        // 用于输出信息
das_module_group *module_group = das_modulegroup_make(); // 模块组
das_file_access *file_access = das_fileaccess_make_default(); // 文件访问器

// 3. ★★★ 关键步骤:将字符串注册为虚拟文件 ★★★
//    参数: 文件访问器, 虚拟文件名, 源代码字符串, 0 表示借用字符串(不复制)
das_fileaccess_introduce_file(file_access, "script.das", SCRIPT_SOURCE, 0);

// 4. 编译虚拟文件
das_program *program = das_program_compile(
    (char*)"script.das",           // 虚拟文件名
    file_access,            // 文件访问器
    tout,                   // 文本输出
    module_group            // 模块组
);

// 5. 检查编译错误
if (das_program_err_count(program)) {
    printf("Compilation failed!\n");
    // 可以遍历并打印具体错误信息
    for (int i = 0; i < das_program_err_count(program); i++) {
        char buf[1024];  // 用于存放格式化后的错误信息
        das_error *err = das_program_get_error(program, i);
        das_error_report(err, buf, sizeof(buf));  // 将错误信息写入 buf
        printf("%s\n", buf);
    }
    das_program_release(program);
    das_fileaccess_release(file_access);
    das_modulegroup_release(module_group);
    das_text_release(tout);
    das_shutdown();
    return -1;
}

// 6. 创建运行时上下文并执行模拟
das_context *ctx = das_context_make(das_program_context_stack_size(program));
if (!das_program_simulate(program, ctx, tout)) {
    printf("Simulation failed!\n");
    das_context_release(ctx);
    das_program_release(program);
    das_fileaccess_release(file_access);
    das_modulegroup_release(module_group);
    das_text_release(tout);
    das_shutdown();
    return -1;
}

// 7. 查找并调用导出的函数
das_function *fn_test = das_context_find_function(ctx, "main");
if (!fn_test) {
    printf("Function 'test' not found!\n");
} else {

    // 无参数函数,第三个参数传 NULL
    auto start = high_resolution_clock::now();
    das_context_eval_with_catch(ctx, fn_test, NULL);
    auto end = high_resolution_clock::now();
    auto duration = duration_cast<microseconds>(end - start).count();
    std::cout << "耗时: " << duration << " 微秒" << std::endl;

    // 检查是否有异常抛出
    char *exception = das_context_get_exception(ctx);
    if (exception) {
        printf("Exception: %s\n", exception);
    }
}

//das_function *fn_add = das_context_find_function(ctx, "add");
//if (!fn_add) {
//    printf("Function 'add' not found!\n");
//} else {
//    vec4f args[2];
//    args[0] = das_result_int(10);   // 第一个参数
//    args[1] = das_result_int(20);   // 第二个参数
//    auto result=das_context_eval_with_catch(ctx, fn_add, args);
//    // 检查是否有异常抛出
//    char *exception = das_context_get_exception(ctx);
//    if (exception) {
//        printf("Exception: %s\n", exception);
//    }
//    int sum = das_argument_int(result);
//    printf("%d\n", sum);
//}

// 8. 释放所有资源 (顺序很重要)
das_context_release(ctx);
das_program_release(program);
das_fileaccess_release(file_access);
das_modulegroup_release(module_group);
das_text_release(tout);
das_shutdown();

return 0;

}

#daslang code:
options gen2=true
options gc=true
options jit_enabled=true

[jit]
def test(var a){
for( i in range(1,100000001)){
a=a+i
}
return a
}

[export]
def main() {
//if (jit_enabled()) {
// print("JIT is enabled!\n");
//} else {
// print("JIT is NOT enabled.\n");
//}
var a=test(1)
print("{a}\n")
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions