我在使用emscripten编译的C程序中打开用户指定的文件时遇到问题(并且正在通过节点运行)。采取这个简单的cat
- 类程序:
#include <stdio.h>
#define BUFSIZE 100
int main( int argc, char *argv[] )
{
char *filename;
FILE *file;
char buf[BUFSIZE];
if (argc != 2) {
fprintf (stderr, "Usage: %s <filename>\n", argv[0]);
return 1;
}
file = fopen (argv[1], "r");
while (fgets (buf, BUFSIZE, file))
fputs (buf, stdout);
fclose (file);
return 0;
}
我使用emscripten成功编译了这个:
% /usr/local/src/emscripten/emcc tsrc/mycat.c
clang: warning: argument unused during compilation: '-nostdinc++'
它运行,并期望一个参数:
% node a.out.js
Usage: /bin/this.program <filename>
但是当我给它那个论点时,它就是barfs:
% node a.out.js somefile
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
TypeError: Function.prototype.apply: Arguments list has wrong type
at Function.APPLY_PREPARE (native)
at Pointer_stringify (/path/to/mycat/a.out.js:624:34)
at _fopen (/path/to/mycat/a.out.js:1917:14)
at Object._main (/path/to/mycat/a.out.js:2531:15)
at Object.callMain (/path/to/mycat/a.out.js:2585:25)
at doRun (/path/to/mycat/a.out.js:2624:20)
at run (/path/to/mycat/a.out.js:2647:12)
at Object.<anonymous> (/path/to/mycat/a.out.js:2663:1)
at Module._compile (module.js:441:26)
at Object..js (module.js:459:10)
这是编译后的JavaScript:
function Pointer_stringify(ptr, /* optional */ length) {
// Find the length, and check for UTF while doing so
var hasUtf = false;
var t;
var i = 0;
while (1) {
t = HEAPU8[(((ptr)+(i))|0)];
if (t >= 128) hasUtf = true;
else if (t == 0 && !length) break;
i++;
if (length && i == length) break;
}
if (!length) length = i;
var ret = '';
if (!hasUtf) {
var MAX_CHUNK = 1024; // split up into chunks, because .apply on a huge string can overflow the stack
var curr;
while (length > 0) {
curr = String.fromCharCode.apply(String, HEAPU8.subarray(ptr, ptr + Math.min(length, MAX_CHUNK))); // ERROR OCCURS ON THIS LINE
ret = ret ? ret + curr : curr;
ptr += MAX_CHUNK;
length -= MAX_CHUNK;
}
return ret;
}
var utf8 = new Runtime.UTF8Processor();
for (i = 0; i < length; i++) {
assert(ptr + i < TOTAL_MEMORY);
t = HEAPU8[(((ptr)+(i))|0)];
ret += utf8.processCChar(t);
}
return ret;
}
错误消息(第624行)中的问题行是String.fromCharCode.apply
的问题行。
相关软件的版本:
% clang --version
clang version 3.2 (tags/RELEASE_32/final)
...
% node --version
v0.6.15
% python --version
Python 2.7.1
% /usr/local/src/emscripten/emcc --version
emcc (Emscripten GCC-like replacement) 1.3.6 (commit 17da251d334ce62d633d51f874b92e19ad9dbf45)
...
最终我想在浏览器环境中加载文件......我知道这是一个完全不同的球赛,有文件预加载等....现在我只想让命令行工作!
更新后添加:查看https://github.com/kripken/emscripten/wiki/Filesystem-Guide后,我尝试预加载文件&amp;得到一个不同的错误:
% /usr/local/src/emscripten/emcc mycat.c --preload-file somefile
...
% node a.out.js somefile
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
ReferenceError: XMLHttpRequest is not defined
at...
这是否意味着我需要在浏览器中运行它?
再次更新以添加:
% /usr/local/src/emscripten/emcc mycat.c -o mycat.html --preload-file README.md
clang: warning: argument unused during compilation: '-nostdinc++'
% open mycat.html
浏览器窗口显示“正在准备...”,控制台日志显示错误:
XMLHttpRequest cannot load file://localhost/path/to/mycat.data. Cross origin requests are only supported for HTTP. mycat.html:1
Uncaught Error: NETWORK_ERR: XMLHttpRequest Exception 101 mycat.html:2816
still waiting on run dependencies: mycat.html:61
dependency: fp somefile mycat.html:61
dependency: datafile_mycat.data mycat.html:61
(end of list)
...然后是最后四行 - 从'仍在等待运行依赖项:mycat.htm'开始 - 每隔几秒重复一次。
我是否需要在javascript端使用FS准备这些文件系统调用,不知何故?
答案 0 :(得分:0)
你快到了。错误仅支持HTTP的跨源请求只是意味着您的浏览器不允许直接从您的硬盘加载网页。
某些浏览器支持命令行参数来启用此功能,但我强烈建议您只在简单的HTTP服务器上运行网页。使用Python很容易实现:
python -m SimpleHTTPServer
http://127.0.0.1:8000