4

我在使用 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>

但是当我给它这个论点时,它会抱怨:

% 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后,我尝试预加载文件并得到不同的错误:

% /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 

浏览器窗口显示“Preparing...”,控制台日志显示错误:

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 准备这些文件系统调用?

4

1 回答 1

1

你快到了。错误Cross origin requests are only supported for HTTP仅表示您的浏览器不允许直接从硬盘加载网页。

一些浏览器支持命令行参数来启用此功能,但我强烈建议您只需在简单的 HTTP 服务器上运行您的网页。这很容易使用 Python 实现:

  1. 在您的应用程序文件夹中打开命令提示符
  2. 执行python -m SimpleHTTPServer
  3. 将浏览器指向http://127.0.0.1:8000
于 2015-11-24T22:07:01.617 回答