# Help with Candid File construction

**URL:** <https://forum.dfinity.org/t/help-with-candid-file-construction/1160>\
**Category:** Language Support\
**Tags:** Wasm\
**Created:** [August 28, 2020, 7:36pm UTC](https://forum.dfinity.org/t/help-with-candid-file-construction/1160 "2020-08-28T19:36:17Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)\
**Post date:** [August 28, 2020, 7:36pm UTC](https://forum.dfinity.org/t/help-with-candid-file-construction/1160/1 "2020-08-28T19:36:17Z")

</div>

I found this project [https://mbbill.github.io/JSC.js/](https://mbbill.github.io/JSC.js/) that is a web assembly compilation of javascript core. It only has a few functions that I think I can pull out of the c++(code at [https://github.com/mbbill/JSC.js/tree/master/Source/JavaScriptCore](https://github.com/mbbill/JSC.js/tree/master/Source/JavaScriptCore)) the main functions that I need. So I think I have the wasm and the js that I’d need to run the thing(basically it pushes a js string into the engine, executes it, and reads out the results with a couple of memory allocation functions along the way.). I think those are js\_eval, stackAlloc, stackRestore, stackSave…maybe some others.

I understand that this is a terrible idea, and that running a js engine on top of wasm defeats the point of having a wasm engine…but I want to try it anyway just so that I can say that I can. There is a lot of js code out there and it might be interesting to push out canisters that do needed things without having to do a rewrite.

Anyway the function signatures look pretty simple…basically just char\* and integers of various sizes, but I don’t really have any clue where to start with the candid files and what is/isn’t important to implement. Any suggestions? Anyone want to help me with it?

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [August 29, 2020, 9:30pm UTC](https://forum.dfinity.org/t/help-with-candid-file-construction/1160/2 "2020-08-29T21:30:21Z")

</div>

To interoperate on the Internet Computer, the canister needs to understand the system API and the Candid serialization format. It cannot run an arbitrary Wasm module. To get started, I would recommend checking out the reverse example in C: [https://github.com/dfinity/examples/blob/master/c/reverse/reverse.c](https://github.com/dfinity/examples/blob/master/c/reverse/reverse.c)

It contains the minimal bindings needed to reverse a string from a canister. Your Wasm module needs to somehow embed these APIs as well in order to run on the Internet Computer.

---

<div class="post-metadata">

**Author:** ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)\
**Post date:** [September 30, 2020, 9:28pm UTC](https://forum.dfinity.org/t/help-with-candid-file-construction/1160/3 "2020-09-30T21:28:28Z")

</div>

I tried going down this road and ended up with this issue: [https://github.com/dfinity/examples/issues/48](https://github.com/dfinity/examples/issues/48)

Any help would be appreciated!

---

<div class="post-metadata">

**Author:** ![Ori](https://avatars.discourse-cdn.com/v4/letter/o/ecae2f/32.png) [@Ori](https://forum.dfinity.org/u/Ori)\
**Post date:** [October 1, 2020, 10:15am UTC](https://forum.dfinity.org/t/help-with-candid-file-construction/1160/4 "2020-10-01T10:15:43Z")

</div>

Make sure you’re using consistent versions across the toolchain. Eg

`sudo apt-get install clang-11 lldb-11 lld-11`

And the correct version of wasm-ld is being called. You could use update-alternatives:

`sudo update-alternatives --install /usr/bin/wasm-ld wasm-ld /usr/bin/wasm-ld-11 100`

(Also check your wcc alias is pointing at clang-11).

---

<div class="post-metadata">

**Author:** ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)\
**Post date:** [October 1, 2020, 3:29pm UTC](https://forum.dfinity.org/t/help-with-candid-file-construction/1160/5 "2020-10-01T15:29:27Z")

</div>

clang-11 in the wcc was the issue. Thanks!

---

<div class="post-metadata">

**Author:** ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)\
**Post date:** [October 1, 2020, 6:34pm UTC](https://forum.dfinity.org/t/help-with-candid-file-construction/1160/6 "2020-10-01T18:34:23Z")

</div>

So made some progress on this and decided to try to go with the duktape project to try to get something to work. I’m using the basic reverse.c example but I moved over the duktape.h and duktape.c files into the directory and changed the function to try to call something out of this. It turns out that the reason it can’t compile is that it can’t see the standard libraries for some reason.

```
root@ad0ec66809cb:/usr/src/examples/c/reverse# wcc reverse.c
In file included from reverse.c:1:
In file included from ./duktape.h:196:
./duk_config.h:865:10: fatal error: 'time.h' file not found
#include <time.h>
         ^ ~~~~~~~
1 error generated.

```

time.h is in /usr/include. Do I need to do something to tell the compiler to look there?

I’m currently running clang-11 --target=wasm32 -c -O reverse.c

The code I’m trying to compile is at [https://github.com/skilesare/examples/blob/master/c/reverse/reverse.c](https://github.com/skilesare/examples/blob/master/c/reverse/reverse.c)

---

<div class="post-metadata">

**Author:** ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)\
**Post date:** [October 2, 2020, 4:39pm UTC](https://forum.dfinity.org/t/help-with-candid-file-construction/1160/7 "2020-10-02T16:39:50Z")

</div>

So a bit of progress! I got things to compile, but when trying to install the canister I get the following:

Installing code for canister reverse, with canister\_id 75hes-oqbaa-aaaaa-aaaaa-aaaaa-aaaaa-aaaaa-q  
Replica error (code 5): IC0504: Canister 75hes-oqbaa-aaaaa-aaaaa-aaaaa-aaaaa-aaaaa-q violated contract: Module imports function ‘duk\_create\_heap’ from ‘env’ that is not exported by the runtime.

I’m guessing what I need is to better understand what all of these do:

#define WASM\_IMPORT(m,n) **attribute** ((import\_module(m))) **attribute** ((import\_name(n)));  
#define WASM\_EXPORT(n) asm(n) **attribute** ((visibility(“default”)))

int dfn\_ads(void) WASM\_IMPORT(“ic0”, “msg\_arg\_data\_size”);  
void dfn\_adc(void \*, int, int) WASM\_IMPORT(“ic0”, “msg\_arg\_data\_copy”);  
void dfn\_reply\_append(void \*, int) WASM\_IMPORT(“ic0”, “msg\_reply\_data\_append”);  
void dfn\_reply(void) WASM\_IMPORT(“ic0”, “msg\_reply”);  
void dfn\_print(void \*, int) WASM\_IMPORT(“ic0”, “debug\_print”);

I’m guessing I need to import the duk\_create\_heap function to some other different name and call it that way? But I’ve tried this a couple different ways and received the compiler error:

reverse.c:23:16: error: initializing ‘duk\_context \*’ (aka ‘struct duk\_hthread \*’) with an expression of incompatible type ‘void’  
duk\_context \*ctx = dfx\_js\_create\_heap\_default();

for both

void dfx\_js\_create\_heap\_default(void) WASM\_IMPORT(“ic0”, “duk\_create\_heap”);

and

void dfx\_js\_create\_heap\_default() WASM\_IMPORT(“ic0”, “duk\_create\_heap”);

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [October 2, 2020, 8:00pm UTC](https://forum.dfinity.org/t/help-with-candid-file-construction/1160/8 "2020-10-02T20:00:39Z")

</div>

This error means `duk_create_heap` is not linked in the Wasm module, so it expects the system to provide the imports, which doesn’t exist.

I don’t know how duktape works, but the problem is probably that they are not compiled properly in Wasm. I suggest you first try to compile a wasm module that can be instantiated in browser or WASI, then try to port it to IC. The porting to IC would be similar to the `reverse.c` example, where it puts wasm\_import and candid bindings.

---

<div class="post-metadata">

**Author:** ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)\
**Post date:** [October 9, 2020, 7:06pm UTC](https://forum.dfinity.org/t/help-with-candid-file-construction/1160/9 "2020-10-09T19:06:23Z")

</div>

I’ve found a WASI implementation and I’ve created an issue over there to try to decipher the bindings: [https://github.com/saghul/wasi-lab/issues/7](https://github.com/saghul/wasi-lab/issues/7)
