2
2

More than 5 years have passed since last update.

emscripten memo

Posted at

ビルド

cd path/to/repo
cd zlib
emconfigure ./configure
make
cd ..
emcc -O2 --closure 0 src/zpipe.c zlib/libz.a -o dist/zpipe.raw.js
cat src/header.js dist/zpipe.raw.js src/footer.js > zpipe.js
java -jar path/to/compiler.jar --compilation_level ADVANCED_OPTIMIZATIONS --js dist/zpipe.js --js_output_file dist/zpipe.min.js

ソース

#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "zlib.h"

// ...

int def(FILE *source, FILE *dest, int level, int zlib_header)
{
// ...
}

int inf(FILE *source, FILE *dest, int zlib_header)
{
// ...
}

int def_stdio(int level, int zlib_header) {
    SET_BINARY_MODE(stdin);
    SET_BINARY_MODE(stdout);
    return def(stdin, stdout, level, zlib_header);
}

int inf_stdio(int zlib_header) {
    SET_BINARY_MODE(stdin);
    SET_BINARY_MODE(stdout);
    return inf(stdin, stdout, zlib_header);
}
//------------------ header.js -------------------
//グローバル汚染しまくるので囲う。
(function() {

//名前が被ったらいやなので一応プレフィックスつけとく。
var zpipeInput;
var zpipeOutput = new Uint8Array(1);
var zpipeTemp;
var zpipeInputIndex;
var zpipeOutputIndex;

var Module = {
  //標準入力を上書き
  'stdin': function() {
    return ++zpipeInputIndex < zpipeInput.length ?
      zpipeInput[zpipeInputIndex] :
      null;
  },

  //標準出力を上書き
  'stdout': function(x) {
    if (x !== null) {
      if (++zpipeOutputIndex === zpipeOutput.length) {
        zpipeTemp = new Uint8Array(zpipeOutput.length * 2);
        zpipeTemp.set(zpipeOutput);
        zpipeOutput = zpipeTemp;
      }
      zpipeOutput[zpipeOutputIndex] = x;
    }
  }
};
//------------------ /header.js -------------------

// zpipe.raw.js

//------------------ footer.js -------------------
function zpipeInit(input) {
  zpipeInputIndex = -1;
  zpipeOutputIndex = -1;
  zpipeInput = input;
  //標準入力を使いまわす場合、eofフラグは一度trueになると元に戻らないので手動で入力。
  FS.streams[1].eof = false;
}

function zpipeGetOutput() {
  return new Uint8Array(zpipeOutput.subarray(0, ++zpipeOutputIndex));
}

function zpipeDeflate(input, level, zlibHeader) {
  zpipeInit(input);

  level = typeof level === 'number' ? level : 6;
  level = Math.min(Math.max(level, 0), 9);
  zlibHeader = zlibHeader ? 1 : -1;

  _def_stdio(level, zlibHeader);
  return zpipeGetOutput();
}

function zpipeInflate(input, zlibHeader) {
  zpipeInit(input);
  zlibHeader = zlibHeader ? 1 : -1;
  _inf_stdio(zlibHeader);
  return zpipeGetOutput();
}

function zpipeGC() {
  zpipeInput = new Uint8Array(1);
}

// export

var global = (function() {return this})();

global['zpipe'] = global['zpipe'] || {};
global['zpipe']['deflate'] = zpipeDeflate;
global['zpipe']['inflate'] = zpipeInflate;
global['zpipe']['gc'] = zpipeGC;

})();
//------------------ /footer.js -------------------

stdin,stdoutが重い?

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2