Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
148 views
in Technique[技术] by (71.8m points)

html - Why can't this website find the Module javascript object

I have compiled 2 c functions to wasm and when I try to use them in a simple website I get this error in the console ReferenceError: Can't find variable: Module:

My c Code

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "argon2.h"
#include "emscripten.h"

EMSCRIPTEN_KEEPALIVE
const int secret(const int key, const int iters)
{
    ...
}

EMSCRIPTEN_KEEPALIVE
const char *argon_hash(const char *SALT, const char *PWD, const uint32_t HASHLEN, const uint32_t LOWERS,
                       const uint32_t UPPERS, const uint32_t NUMS, const uint32_t SPCLS)
{
    ...
}

Here is my compilation script

emcc -std=gnu99 -O3 -flto --closure 1 
-I./argon2/include -I./argon2/src -DARGON2_NO_THREADS=1 
./argon2/src/argon2.c ./argon2/src/core.c ./argon2/src/blake2/blake2b.c 
./argon2/src/thread.c ./argon2/src/encoding.c ./argon2/src/ref.c 
-s INLINING_LIMIT=1 -s NO_EXIT_RUNTIME=1 -s ALLOW_MEMORY_GROWTH=1 -s FILESYSTEM=0 
-s EXPORTED_FUNCTIONS='["_secret", "_argon_hash"]' -s EXPORTED_RUNTIME_METHODS='["cwrap", "ccall"]' 
argon2-web.c -o argon2-web.js

This generates argon2-web.js as well as argon2-web.wasm

index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Simple template</title>
    <script src="./argon2-web.js"></script>
  </head>
  <body>
    <script src="./web_test.js"></script>
    <p>Hello World</p>
  </body>
</html>

web_test.js

secret = Module.cwrap("secret", "number", [ "number", "number" ]);
argon_hash =
    Module.cwrap("argon_hash", "string", [ "string", "string", "number", "number", "number", "number", "number" ]);

function makePass(salt, pass, len = 16, key = 1, lowers = 1, uppers = 1, numerics = 1, specials = 1)
{
    var slt_sct = salt.concat(secret(key, len).toString());
    var hash = argon_hash(slt_sct, pass, len, lowers, uppers, numerics, specials);
    return hash;
}

var pass = makePass('The Salt', 'The Password');
console.log(pass)
document.body.innerHTML = '<h1>The Password is ' + pass + '</h1>'
question from:https://stackoverflow.com/questions/65926220/why-cant-this-website-find-the-module-javascript-object

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...