New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

c2wasm

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

c2wasm

compile c to wasm, the easy way

latest
Source
npmnpm
Version
1.0.3
Version published
Maintainers
1
Created
Source

c2wasm

I jumped through all the hoops for you, so you can convert c to wasm.

pieced together from various clues:

  • prebuilt wasm producing clang yurydelendik/clang-heroku-slug (as used in webassembly-explorer)
  • hints about compiler and linker args from aransentin
  • also from wasmception
  • inspecting output with wabt
  • learning the raw web assembly format from webassembly semantics

performance optimizations

The first output I generated was surprisingly bloated, (seemed to be inlining too much or something?, creating lots of i32 variables) also, wasn't faster than my carefully optimized javascript. but turns out clang has lots of optimization options - I ended up using -O3

imports and exports

wasm method, accessable from javascript

//make this weird macro
#define export __attribute__ ((visibility ("default")))

//then at least this looks reasonable.
export
int add (int a, int b) {
  return a + b
}

compile to wasm

cd examples/add
c2wasm add.c -o add.wasm

see ./example/add/add.js for how to call this from javascript

javascript method, accessable from wasm

#define export __attribute__ ((visibility ("default")))

//since this is undefined, it's treated as an import
void console_log (char* string, int length);

export
void hello () {
  char string[]="hello world";
  console_log(&string, sizeof(string));
}

compile the same as before, and again, see ./examples/hello/hello.js for how to call from javascript, and pass import in from javascript too.

future work

it's kinda lame to be downloading (and compiling) all these nasty c compilers to build a tiny bit of wasm. isn't the point of wasm to be an actually portable binary target? why can't we compile clang to wasm and then install clang.wasm from npm?

clang-in-browser does that. somebody should wrap that into a node module!

License

MIT

FAQs

Package last updated on 11 Dec 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts