emscripten-core / emscripten

Emscripten: An LLVM-to-WebAssembly Compiler
Other
25.83k stars 3.31k forks source link

How to load module correctly (error: module is not an object or function)? #13387

Closed paulocoutinhox closed 1 month ago

paulocoutinhox commented 3 years ago

Hi guys. Im trying load WASM module here: https://paulo-coutinho.github.io/pdfium-lib/

But you can see on log/console that is throwing error: Uncaught (in promise) TypeError: WebAssembly.instantiate(): Import #0 module="a" error: module is not an object or function

I tried a lot of things to make it work as standalone wasm module.

My html code is here: https://github.com/paulo-coutinho/pdfium-lib/blob/support-wasm/extras/wasm/template/index.html

Can anyone help me?

kripken commented 3 years ago

If the module name is "a" then I think the wasm has been optimized as non-standalone. That is, it has had its imports minified.

You should build with the flag to make it standalone, -s STANDALONE_WASM. See also https://v8.dev/blog/emscripten-standalone-wasm

paulocoutinhox commented 3 years ago

Ok. I will try now.

But i don't understand three things. Can you answer me?

  1. The standalone mode let me use it without a browser with wasmer for example? But it cannot be used on web like im doing now?
  2. What is the "correct" compile mode to make it be used optimised for web? Im using today these flags inside pdfium.rsp:
    -s DEMANGLE_SUPPORT=1
    -s USE_PTHREADS=1
    -s USE_ZLIB=1
    -s USE_LIBJPEG=1
    -s WASM=1

And when compile:

"emcc",
"-o",
"index.html",
"-s",
'EXPORTED_FUNCTIONS="$(node function-names ../xml/index.xml createDocFromBuffer)"',
"-s",
'EXTRA_EXPORTED_RUNTIME_METHODS=\'["ccall", "cwrap"]\'',
"--pre-js pre-js.js",
"avail.c",
"@pdfium.rsp",
"-Os",
  1. Without standalone mode what is the correct way/flow to generate a library that others can use on their website and in my web demo site?

Thanks.

paulocoutinhox commented 3 years ago

And number 4:

emcc: error: STANDALONE_WASM does not support pthreads yet
paulocoutinhox commented 3 years ago

Im trying use webpack to create a html page that load is show results only. But it too hard.

> pdfium@ build /Users/paulo/Developer/workspaces/cpp/pdfium-lib/build/linux/x64/release/final
> webpack

Hash: ba8ed7e3be9b423e6fde
Version: webpack 4.43.0
Time: 2669ms
Built at: 01/30/2021 1:15:51 AM
                                Asset      Size  Chunks  Chunk Names
1ebec6b98d003f6611cef49fe4887488.wasm  3.64 MiB          
                              main.js  1.49 MiB    main  main
Entrypoint main = main.js
  [2] ./node_modules/process/browser.js 5.29 KiB {main} [built]
  [3] ./node_modules/buffer/index.js 47.5 KiB {main} [built]
  [4] (webpack)/buildin/global.js 472 bytes {main} [built]
 [14] ./node_modules/randombytes/browser.js 1.54 KiB {main} [built]
 [20] ./node_modules/create-hash/browser.js 625 bytes {main} [built]
 [25] ./node_modules/path-browserify/index.js 8.54 KiB {main} [built]
 [54] ./node_modules/isarray/index.js 132 bytes {main} [built]
 [55] ./node_modules/node-libs-browser/mock/empty.js 0 bytes {main} [built]
 [69] ./node_modules/create-hmac/browser.js 1.55 KiB {main} [built]
 [72] ./node_modules/pbkdf2/browser.js 83 bytes {main} [built]
[108] ./pdfium.js 191 KiB {main} [built]
[109] ./pdfium.wasm 62 bytes {main} [built]
[110] ./index.js 545 bytes {main} [built]
[111] ./node_modules/base64-js/index.js 3.84 KiB {main} [built]
[113] ./node_modules/crypto-browserify/index.js 2.87 KiB {main} [optional] [built]
    + 197 hidden modules

WARNING in ./pdfium.js
Module not found: Error: Can't resolve 'worker_threads' in '/Users/paulo/Developer/workspaces/cpp/pdfium-lib/build/linux/x64/release/final'
 @ ./pdfium.js
 @ ./index.js

WARNING in configuration
The 'mode' option has not been set, webpack will fallback to 'production' for this value. Set 'mode' option to 'development' or 'production' to enable defaults for each environment.
You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/configuration/mode/

ERROR in ./pdfium.js
Module not found: Error: Can't resolve 'perf_hooks' in '/Users/paulo/Developer/workspaces/cpp/pdfium-lib/build/linux/x64/release/final'
 @ ./pdfium.js 1:4580-4601
 @ ./index.js

ERROR in main.js from Terser
"module" is redeclared [main.js:34910,6]

webpack config

module.exports = {
  // mode: "development || "production",
  entry: "./index.js",
  output: {
    webassemblyModuleFilename: "[hash].wasm",
    publicPath: "dist/"
  },
  module: {
    rules: [
      {
        test: /pdfium\.wasm$/,
        type: "javascript/auto",
        loader: "file-loader",
        options: {
          publicPath: "dist/"
        }
      }
    ]
  },
  optimization: {
    chunkIds: "named"
  },
  node: {
    "fs": "empty"
  },
};

package.json

{
    "name": "pdfium",
    "scripts": {
        "build": "webpack",
        "serve": "http-server",
        "start": "npm run build && npm run serve"
    },
    "devDependencies": {
        "exports-loader": "0.7.0",
        "file-loader": "1.1.11",
        "http-server": "0.12.3",
        "webpack": "4.43.0",
        "webpack-cli": "4.4.0"
    }
}

Im posting all here because we have different timezone. If you can, please answer from the first. Thanks.

paulocoutinhox commented 3 years ago

Hi,

I made a patch and compiled "libpdfium.a" without pthread. So i don't need it now. I removed all USE_PTHREAD.

So, backing to standalone: I use it now, it compile, but when it will be compiled i got:

Uncaught (in promise) TypeError: WebAssembly.instantiate(): Import #5 module="wasi_snapshot_preview1" error: module is not an object or function

I need do something special?

Im posting all here because we have different timezone. If you can, please answer from the first. Thanks.

paulocoutinhox commented 3 years ago

Hi,

Finally i made it works after a lot of work:

https://paulo-coutinho.github.io/pdfium-lib/

My project: https://github.com/paulo-coutinho/pdfium-lib

Thanks.

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because there has been no activity in the past year. It will be closed automatically if no further activity occurs in the next 30 days. Feel free to re-open at any time if this issue is still relevant.

Havunen commented 1 month ago
Hi,

Finally i made it works after a lot of work:

https://paulo-coutinho.github.io/pdfium-lib/

My project:
https://github.com/paulo-coutinho/pdfium-lib

Thanks.

I'm getting the same error, what was the solution?

paulocoutinhox commented 1 month ago

You can get current code here: https://github.com/paulocoutinhox/pdfium-lib