[js] some refactoring

dev
MITSUNARI Shigeo 7 years ago
parent 18ac8a55d2
commit ee249d839f
  1. 72
      docs/demo/she.js

@ -1,8 +1,10 @@
(function(generator) { (function(generator) {
if (typeof exports === 'object') { if (typeof exports === 'object') {
exports.mod = require('./she.js')
generator(exports, true) generator(exports, true)
} else { } else {
let exports = {} let exports = {}
exports.mod = {}
window.she = generator(exports, false) window.she = generator(exports, false)
} }
})(function(exports, isNodeJs) { })(function(exports, isNodeJs) {
@ -24,64 +26,48 @@
const SHE_CIPHERTEXT_G2_SIZE = MCLBN_G2_SIZE * 2 const SHE_CIPHERTEXT_G2_SIZE = MCLBN_G2_SIZE * 2
const SHE_CIPHERTEXT_GT_SIZE = MCLBN_GT_SIZE * 4 const SHE_CIPHERTEXT_GT_SIZE = MCLBN_GT_SIZE * 4
let mod = {}
let capi = {}
let g_callback = null let g_callback = null
let g_range = 1024 let g_range = 0
let g_tryNum = 1024 let g_tryNum = 0
exports.mod = mod
exports.capi = capi
if (isNodeJs) { let capi = {}
mod = require('./she.js') exports.capi = capi
let mod = exports.mod
exports.init = function(callback) { exports.init = function(callback = null, range = 1024, tryNum = 1024) {
console.log('onModuleInit') console.log('init')
g_callback = callback g_callback = callback
g_range = range
g_tryNum = tryNum
if (isNodeJs) {
} else {
fetch('mclshe.wasm')
.then(response => response.arrayBuffer())
.then(buffer => new Uint8Array(buffer))
.then(binary => { Module(mod) })
} }
mod.onRuntimeInitialized = function () { }
const fs = require('fs')
const json = fs.readFileSync('./exported-she.json') mod.onRuntimeInitialized = function () {
exportedFuncs = JSON.parse(json) const f = function(exportedFuncs) {
exportedFuncs.forEach(func => { exportedFuncs.forEach(func => {
capi[func.exportName] = mod.cwrap(func.name, func.returns, func.args) capi[func.exportName] = mod.cwrap(func.name, func.returns, func.args)
}) })
define_extra_functions(mod) define_extra_functions(mod)
capi.sheInit() capi.sheInit()
console.log('initializing sheSetRangeForDLP') console.log('initializing sheSetRangeForDLP')
let range = 1024 const r = capi.sheSetRangeForDLP(g_range, g_tryNum)
let tryNum = 512
let r = capi.sheSetRangeForDLP(range, tryNum)
console.log('finished ' + r) console.log('finished ' + r)
if (g_callback) g_callback() if (g_callback) g_callback()
} }
} else { if (isNodeJs) {
exports.init = function(callback = null, range = 1024, tryNum = 1024) { const fs = require('fs')
g_callback = callback const jsonStr = fs.readFileSync('./exported-she.json')
g_range = range f(JSON.parse(jsonStr))
g_tryNum = tryNum } else {
fetch('mclshe.wasm')
.then(response => response.arrayBuffer())
.then(buffer => new Uint8Array(buffer))
.then(binary => {
Module(mod)
})
}
mod.onRuntimeInitialized = function() {
fetch('exported-she.json') fetch('exported-she.json')
.then(response => response.json()) .then(response => response.json())
.then(json => { .then(exportedFuncs => f(exportedFuncs))
mod.json = json
json.forEach(func => {
capi[func.exportName] = mod.cwrap(func.name, func.returns, func.args)
})
define_extra_functions(mod)
capi.sheInit()
console.log('initializing sheSetRangeForDLP')
let r = capi.sheSetRangeForDLP(g_range, g_tryNum)
console.log('finished ' + r)
if (g_callback) g_callback()
})
} }
} }

Loading…
Cancel
Save