Help us understand the problem. What is going on with this article?

emscripten memo

More than 5 years have passed since last update.

https://github.com/ukyo/zpipe

ビルド

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が重い?

ukyo
こんにちはちは
https://ukyoweb.com
wacul
人工知能でWebサイトの課題を発見する AIアナリスト https://wacul-ai.com を開発しています
https://wacul.co.jp
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away