Skip to content

Instantly share code, notes, and snippets.

@slmyers
Created March 7, 2017 05:08
Show Gist options
  • Save slmyers/54fe9f1814ef9557a76727d07a23464e to your computer and use it in GitHub Desktop.
Save slmyers/54fe9f1814ef9557a76727d07a23464e to your computer and use it in GitHub Desktop.
Error throwing wasm sqlite
#chrome Version 57.0.2984.0 dev (64-bit)
Uncaught TypeError: Cannot read property 'apply' of undefined
at Object.Module.stackAlloc (sql-debug.js:6069)
at sql-debug.js:6388
at sql-debug.js:7229
#firefox 54.0a1 (2017-03-06) (64-bit)
TypeError: Module.asm.stackAlloc is undefined[Learn More]
<body>
<script type='text/javascript'>
Module = {};
Module['_main'] = function () {
var sql = window.SQL;
// or sql = window.SQL if you are in a browser
// Create a database
var db = new sql.Database();
// NOTE: You can also use new sql.Database(data) where
// data is an Uint8Array representing an SQLite database file
// Execute some sql
sqlstr = "CREATE TABLE hello (a int, b char);";
sqlstr += "INSERT INTO hello VALUES (0, 'hello');"
sqlstr += "INSERT INTO hello VALUES (1, 'world');"
db.run(sqlstr); // Run the query without returning anything
var res = db.exec("SELECT * FROM hello");
/*
[
{columns:['a','b'], values:[[0,'hello'],[1,'world']]}
]
*/
// Prepare an sql statement
var stmt = db.prepare("SELECT * FROM hello WHERE a=:aval AND b=:bval");
// Bind values to the parameters and fetch the results of the query
var result = stmt.getAsObject({ ':aval': 1, ':bval': 'world' });
console.log(result); // Will print {a:1, b:'world'}
// Bind other values
stmt.bind([0, 'hello']);
while (stmt.step()) console.log(stmt.get()); // Will print [0, 'hello']
// You can also use javascript functions inside your SQL code
// Create the js function you need
function add(a, b) { return a + b; }
// Specifies the SQL function's name, the number of it's arguments, and the js function to use
db.create_function("add_js", add);
// Run a query in which the function is used
db.run("INSERT INTO hello VALUES (add_js(7, 3), add_js('Hello ', 'world'));"); // Inserts 10 and 'Hello world'
// free the memory used by the statement
stmt.free();
// You can not use your statement anymore once it has been freed.
// But not freeing your statements causes memory leaks. You don't want that.
// Export the database to an Uint8Array containing the SQLite database file
var binaryArray = db.export();
};
var xhr = new XMLHttpRequest();
xhr.open('GET', 'js/sql-debug-raw.wasm', true);
xhr.responseType = 'arraybuffer';
xhr.onload = function () {
console.log(xhr.response);
Module.wasmBinary = xhr.response;
var script = document.createElement('script');
script.src = "js/sql-debug.js";
document.body.appendChild(script);
};
xhr.send(null);
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment