From dccacf2a926150237466a5fe994f2efd9b2681f8 Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Wed, 8 Nov 2017 15:08:53 +0900 Subject: [js] change bls.init api --- docs/demo/bls-demo.js | 12 +++++------ docs/demo/bls.html | 1 - docs/demo/bls.js | 54 ++++++++++++++++++++++---------------------------- docs/demo/bls_c.wasm | Bin 524171 -> 524157 bytes 4 files changed, 30 insertions(+), 37 deletions(-) diff --git a/docs/demo/bls-demo.js b/docs/demo/bls-demo.js index 8214668..926bf11 100644 --- a/docs/demo/bls-demo.js +++ b/docs/demo/bls-demo.js @@ -3,12 +3,12 @@ function setValue(name, val) { document.getElementsByName(name)[0].value = val } function getText(name) { return document.getElementsByName(name)[0].innerText } function setText(name, val) { document.getElementsByName(name)[0].innerText = val } -(function() { - bls.init(function() { - setText('status', 'ok') - setText('curveOrder', bls.capi.blsGetCurveOrder()) - }) -})() +bls.init() + .then(() => { + console.log('XXX') + setText('status', 'ok') + setText('curveOrder', bls.capi.blsGetCurveOrder()) + }) let prevSelectedCurve = -1 function onChangeSelectCurve() { diff --git a/docs/demo/bls.html b/docs/demo/bls.html index 14e09dd..c148f07 100644 --- a/docs/demo/bls.html +++ b/docs/demo/bls.html @@ -4,7 +4,6 @@ BLS signature sample - diff --git a/docs/demo/bls.js b/docs/demo/bls.js index 884956e..10d7f44 100644 --- a/docs/demo/bls.js +++ b/docs/demo/bls.js @@ -28,46 +28,40 @@ const BLS_PUBLICKEY_SIZE = BLS_ID_SIZE * 3 * 2 const BLS_SIGNATURE_SIZE = BLS_ID_SIZE * 3 - let g_callback = null - let g_curveType = 0 - let capi = {} exports.capi = capi let mod = exports.mod - exports.init = function(callback = null, curveType = MCLBN_CURVE_FP254BNB) { + exports.init = (curveType = MCLBN_CURVE_FP254BNB) => { console.log('init') - g_callback = callback - g_curveType = curveType - if (isNodeJs) { - } else { + if (!isNodeJs) { fetch('bls_c.wasm') .then(response => response.arrayBuffer()) .then(buffer => new Uint8Array(buffer)) .then(binary => { Module(mod) }) } - } - - mod.onRuntimeInitialized = function() { - console.log('onRuntimeInitialized') - const f = function(exportedFuncs) { - exportedFuncs.forEach(func => { - capi[func.exportName] = mod.cwrap(func.name, func.returns, func.args) - }) - define_extra_functions(mod) - let r = capi.blsInit(g_curveType) - console.log('finished ' + r) - if (g_callback) g_callback() - } - if (isNodeJs) { - const fs = require('fs') - const jsonStr = fs.readFileSync('./exported-bls.json') - f(JSON.parse(jsonStr)) - } else { - fetch('exported-bls.json') - .then(response => response.json()) - .then(exportedFuncs => f(exportedFuncs)) - } + return new Promise((resolve) => { + mod.onRuntimeInitialized = () => { + const f = (exportedFuncs) => { + exportedFuncs.forEach(func => { + capi[func.exportName] = mod.cwrap(func.name, func.returns, func.args) + }) + define_extra_functions(mod) + let r = capi.blsInit(curveType) + console.log('finished ' + r) + resolve() + } + if (isNodeJs) { + const fs = require('fs') + const jsonStr = fs.readFileSync('./exported-bls.json') + f(JSON.parse(jsonStr)) + } else { + fetch('exported-bls.json') + .then(response => response.json()) + .then(exportedFuncs => f(exportedFuncs)) + } + } + }) } const ptrToStr = function(pos, n) { diff --git a/docs/demo/bls_c.wasm b/docs/demo/bls_c.wasm index 91a2694..e58164d 100644 Binary files a/docs/demo/bls_c.wasm and b/docs/demo/bls_c.wasm differ -- cgit v1.2.3