Skip to content

Instantly share code, notes, and snippets.

@lovasoa
Last active August 29, 2015 14:01
Show Gist options
  • Save lovasoa/3d44eaa8b2f74433f6a3 to your computer and use it in GitHub Desktop.
Save lovasoa/3d44eaa8b2f74433f6a3 to your computer and use it in GitHub Desktop.
bug in emscripten

Emscripten bug #2374

emscripten-core/emscripten#2374

Compile

Just download and run compile.sh

Run

$ node unoptimized.js # Works (returns nothing)
$ node optimized.js # Assertion error
EMSCRIPTEN=/path/to/emscripten/
OPTIONS=-s RESERVED_FUNCTION_POINTERS=2 -s EXPORTED_FUNCTIONS="['_sqlite3_open', '_sqlite3_close', '_sqlite3_exec', '_sqlite3_free', '_sqlite3_data_count', '_sqlite3_column_type', '_sqlite3_column_text', '_sqlite3_column_double', '_sqlite3_prepare_v2', '_sqlite3_step', '_sqlite3_bind_text', '_sqlite3_bind_double', '_sqlite3_reset', '_sqlite3_finalize']"
wget -O sqlite3.c https://raw.githubusercontent.com/lovasoa/sql.js/master/c/sqlite3.c
wget -O post.js https://gist.githubusercontent.com/lovasoa/3d44eaa8b2f74433f6a3/raw/66ec8d477a049fb5ab9b255455ea014bc2dfff54/post.js
$EMSCRIPTEN/emcc $OPTIONS -O0 -g sqlite3.c --post-js post.js -o unoptimized.js
$EMSCRIPTEN/emcc $OPTIONS -O1 -g sqlite3.c --post-js post.js -o optimized.js
// Optimized
function _sqlite3_bind_double($pStmt,$i,$rValue) {
$pStmt = $pStmt|0;
$i = $i|0;
$rValue = +$rValue;
var $0 = 0, $1 = 0, $2 = 0, $3 = 0, $4 = 0, $5 = 0, $6 = 0, $7 = 0, $8 = 0, label = 0, sp = 0;
sp = STACKTOP;
$0 = (_vdbeUnbind($pStmt,$i)|0);
$1 = ($0|0)==(0);
if (!($1)) {
STACKTOP = sp;return ($0|0);
}
$2 = (($i) + -1)|0;
$3 = (($pStmt) + 60|0);
$4 = HEAP32[$3>>2]|0;
$5 = (($4) + (($2*40)|0)|0);
_sqlite3VdbeMemSetDouble($5,$rValue);
$6 = HEAP32[$pStmt>>2]|0;
$7 = (($6) + 12|0);
$8 = HEAP32[$7>>2]|0;
_sqlite3_mutex_leave($8);
STACKTOP = sp;return ($0|0);
}
// Unoptimized
function _sqlite3_bind_double($pStmt,$i,$rValue) {
$pStmt = $pStmt|0;
$i = $i|0;
$rValue = +$rValue;
var $0 = 0, $1 = 0, $10 = 0, $11 = 0, $12 = 0, $13 = 0, $14 = 0, $15 = 0.0, $16 = 0, $17 = 0, $18 = 0, $19 = 0, $2 = 0.0, $20 = 0, $3 = 0, $4 = 0, $5 = 0, $6 = 0, $7 = 0, $8 = 0;
var $9 = 0, $p = 0, $rc = 0, label = 0, sp = 0;
sp = STACKTOP;
STACKTOP = STACKTOP + 32|0;
$0 = $pStmt;
$1 = $i;
$2 = $rValue;
$3 = $0; //@line 66466 "c/sqlite3.c"
$p = $3; //@line 66466 "c/sqlite3.c"
$4 = $p; //@line 66467 "c/sqlite3.c"
$5 = $1; //@line 66467 "c/sqlite3.c"
$6 = (_vdbeUnbind($4,$5)|0); //@line 66467 "c/sqlite3.c"
$rc = $6; //@line 66467 "c/sqlite3.c"
$7 = $rc; //@line 66468 "c/sqlite3.c"
$8 = ($7|0)==(0); //@line 66468 "c/sqlite3.c"
if (!($8)) {
$20 = $rc; //@line 66472 "c/sqlite3.c"
STACKTOP = sp;return ($20|0); //@line 66472 "c/sqlite3.c"
}
$9 = $1; //@line 66469 "c/sqlite3.c"
$10 = (($9) - 1)|0; //@line 66469 "c/sqlite3.c"
$11 = $p; //@line 66469 "c/sqlite3.c"
$12 = (($11) + 60|0); //@line 66469 "c/sqlite3.c"
$13 = HEAP32[$12>>2]|0; //@line 66469 "c/sqlite3.c"
$14 = (($13) + (($10*40)|0)|0); //@line 66469 "c/sqlite3.c"
$15 = $2; //@line 66469 "c/sqlite3.c"
_sqlite3VdbeMemSetDouble($14,$15); //@line 66469 "c/sqlite3.c"
$16 = $p; //@line 66470 "c/sqlite3.c"
$17 = HEAP32[$16>>2]|0; //@line 66470 "c/sqlite3.c"
$18 = (($17) + 12|0); //@line 66470 "c/sqlite3.c"
$19 = HEAP32[$18>>2]|0; //@line 66470 "c/sqlite3.c"
_sqlite3_mutex_leave($19); //@line 66470 "c/sqlite3.c"
$20 = $rc; //@line 66472 "c/sqlite3.c"
STACKTOP = sp;return ($20|0); //@line 66472 "c/sqlite3.c"
}
var assert = require('assert');
var sqlite3_open = Module['cwrap'] ( 'sqlite3_open', 'number', ['string', 'number'] );
var sqlite3_exec = Module['cwrap'] ( 'sqlite3_exec', 'number', ['number', 'string', 'number', 'number', 'number'] );
var sqlite3_prepare_v2 = Module['cwrap'] ( 'sqlite3_prepare_v2', 'number', ['number', 'string', 'number', 'number', 'number'] );
var sqlite3_bind_text = Module['cwrap'] ( 'sqlite3_bind_text', 'number', ['number', 'number', 'string', 'number', 'number'] );
var sqlite3_bind_double = Module['cwrap'] ( 'sqlite3_bind_double', 'number', ['number', 'number', 'number'] );
var sqlite3_step = Module['cwrap'] ('sqlite3_step', 'number', ['number']);
var apiTemp = Runtime.stackAlloc(4);
sqlite3_open(':memory:', apiTemp);
var db = getValue(apiTemp, 'i32');
var sql = "CREATE TABLE test (a text NOT NULL, b real);";
var res = sqlite3_exec(db, sql, 0,0,0);
assert.equal(res, 0);
sql = "INSERT INTO test VALUES (?,?)";
sqlite3_prepare_v2 (db, sql, -1, apiTemp, 0);
var stmt = getValue(apiTemp, 'i32');
sqlite3_bind_text(stmt, 1, "hello", 5, 0);
sqlite3_bind_double(stmt, 2, 6.66); // BUG: This erases the first value, that was bound to "hello"
var res = sqlite3_step(stmt);
var SQLITE_DONE = 101;
assert.equal(res, SQLITE_DONE);
var callback = Runtime.addFunction(function (notUsed, argc, argv, colNames) {
assert.equal('hello', Pointer_stringify(getValue(argv, 'i32')));
assert.equal('6.66', Pointer_stringify(getValue(argv+4, 'i32')));
return 0;
});
sql = "SELECT * FROM test;";
sqlite3_exec(db, sql, callback, 0, 0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment