Change picture:

Choose file:

如何将脚本文件打包编译到道虚拟机可执行文件里


从道语言1.1开始,道虚拟机支持了虚拟文件,允许将保存在字符数组 里的脚本按正常的方式载入,并保持原有的路径结构和路径查找。

首先要做的是用一个 xxd (使用 -i 选项)之类的十六进制转换工具 将脚本文件转换为十六进制码表示的字符串数组。 假定你有两个脚本文件,一个是load.dao,另一个是在module子目录的test.dao。

转换之后将生成如下数组:
unsignedchar  module_test_dao[]  =  {
    0x0a,  0x6c,  0x20,  0x3d,  0x20,  0x28,  ...
    0x6f,  0x6e,  0x67,  0x3e,  0x29,  0x7b,  ...
    0x20,  0x7d,  0x3b,  0x0a,  0x6c,  0x32,  ...
    0x4c,  0x20,  0x3a,  0x20,  0x31,  0x30,  ...
    0x2e,  0x77,  0x72,  0x69,  0x74,  0x65,  ...
    0x20,  0x6c,  0x32,  0x2c,  0x20,  0x28,  ...
    0x2c,  0x20,  0x28,  0x6c,  0x6f,  0x6e,  ...
    0x32,  0x33,  0x34,  0x35,  0x36,  0x37,  ...
    0x35,  0x36,  0x37,  0x38,  0x39,  0x31,  ...
    0x38,  0x39,  0x27,  0x20,  0x29,  0x3b,  ...
    0x54,  0x65,  0x73,  0x74,  0x28,  0x29,  ...
    0x6f,  0x2e,  0x77,  0x72,  0x69,  0x74,  ...
    0x63,  0x61,  0x6c,  0x6c,  0x20,  0x54,  ...
    0x20,  0x29,  0x3b,  0x0a,  0x7d,  0x0a
};
unsignedint  module_test_dao_len  =  162;
unsignedchar  load_dao[]  =  {
    0x0a,  0x6c,  0x6f,  0x61,  0x64,  0x20,  ...
    0x65,  0x73,  0x74,  0x3b,  0x0a,  0x0a,  ...
    0x3b,  0x0a
};
unsignedint  load_dao_len  =  26;
然后再定义另一个数组:
char*  dao_virtual_files[][2]  =
{
    {  "load.dao",  load_dao  },
    {  "module/test.dao",  module_test_dao  },
    {  NULL,  NULL  }
};
这个数组保存了一组路径文件名和相对应文件代码的十六进制字符串表示。 这个数组必须被命名为dao_virtual_files。

这个数组的第一个元素里的文件被认为是主文件,它接受并处理 命令行参数,是开始被执行的脚本。 最后的元素必须由NULL填充,表示此数组已结束。 数组的其他元素可保存第一个脚本所需要的,模块化的脚本文件。 这里的第二个元素的路径文件名为module/test.dao, 这将道语言虚拟机对载入语句"load module.test;"做正确的处理。

最后,包含这些数组的文件可以和道语言虚拟机编译在一起, 这需要使用道虚拟机所提供的daoMainv.c主文件而不是通常使用的 daoMain.c和daoMaindl.c。 要编译在一起,既可直接将这个文件包含在daoMainv.c里, 也可单独编译然后链接在一起。

就这么简单,也许将来会有一个工具提供出来自动地做这种打包处理。
view count 716 times
created at 2009-09-19, 19:08 GMT

123 4
56 78910 11
121314151617 18
192021222324 25
26272829

fu: ... I forgot to say something about the plan for the whole new year in my previous reply. Well, besides w ... (Jan.19,01:40)

fu: ... Happy new dragon year (which will start from this sunday)! Actually, it was a busy month (I wish th ... (Jan.18,22:46)

ybabel: What's the plan for the new year ? Hello 'vry budy :- ) happy new year (when is the new year for you Fu ?) I saw you come back and comm ... (Jan.18,18:59)

This site is powered by Dao
Copyright (C) 2009,2010, daovm.net.
Webmaster: admin@daovm.net