# A javascript stanc3

**URL:** <https://discourse.mc-stan.org/t/a-javascript-stanc3/11044>\
**Category:** Developers\
**Created:** [September 18, 2019, 11:15pm UTC](https://discourse.mc-stan.org/t/a-javascript-stanc3/11044 "2019-09-18T23:15:22Z")\
**Posts on this page:** 18\
**Page:** 2

<div class="post-metadata">

**Author:** ![seantalts](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/seantalts/32/32_2.png) [@seantalts](https://discourse.mc-stan.org/u/seantalts)\
**Post date:** [September 20, 2019, 9:26pm UTC](https://discourse.mc-stan.org/t/a-javascript-stanc3/11044/21 "2019-09-20T21:26:49Z")

</div>

Also I got `ctx$call("stanc", "mn", "data {}")` working for me, though it did error the first 2 times I tried it. No idea what’s going on there.

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [September 20, 2019, 9:28pm UTC](https://discourse.mc-stan.org/t/a-javascript-stanc3/11044/22 "2019-09-20T21:28:41Z")

</div>

OK. That is more or less what we do with the C++ `stanc` now.

Currently,

```
> args(rstan::stanc)
function (file, model_code = "", model_name = "anon_model", verbose = FALSE, 
    obfuscate_model_name = TRUE, allow_undefined = FALSE, isystem = c(if (!missing(file)) dirname(file), 
        getwd())) 

```

which are

- `file` the name of the Stan program on disk, which if specified, I guess we need to read to a string from R
- `model_code` Stan program as a R string
- `model_name` which I guess I can just pass along as the first argument to `stanc`
- `obfuscate_model_name` This made the class a long random string so that the user would not get clashes if they have different models called “my\_model” or something generic. Maybe a better option would be for the class name to be the MD5sum of the Stan program?
- `allow_undefined` tells `stanc` to not throw an error if you have a function that is declared but not defined, so that you can provide the definition in C++
- `isystem` tells `stanc` what directories to look in for the C++ file that defines said functions

The last two are basically the same as in the existing `stanc` (e.g. in CmdStan)

```
stanc version 2.20.0

USAGE: stanc [options] <model_file>

OPTIONS:

  --help Display this information

  --version Display stanc version number

  --name=<string> Model name
                          (default = "$model_filename_model")

  --o=<file> Output file for generated C++ code
                          (default = "$name.cpp")

  --allow_undefined Do not fail if a function is declared but not defined

  --include_paths=<comma-separated list>
                      Comma-separated list of directories that may contain a file in an #include directive

```

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [September 20, 2019, 9:41pm UTC](https://discourse.mc-stan.org/t/a-javascript-stanc3/11044/23 "2019-09-20T21:41:05Z")

</div>

A couple of other things. We need this merged PR

> <https://github.com/stan-dev/stan/issues/2797>

  
for stanc3 as well, so that packages with multiple Stan programs don’t get linker errors from having multiple definitions of `new_model`.

Also, can the `resize_to_match` functions go inside the `namespace`?

Finally, do we need a separate JavaScript file to call `stanfuncs` or can that be done with the existing one?

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [September 20, 2019, 9:51pm UTC](https://discourse.mc-stan.org/t/a-javascript-stanc3/11044/24 "2019-09-20T21:51:40Z")

</div>

Also, how do you get the messages if there is a syntax error in the Stan program, as in

```
ctx$eval("stanc('mn', 'dat {}')")

```

?

---

<div class="post-metadata">

**Author:** ![seantalts](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/seantalts/32/32_2.png) [@seantalts](https://discourse.mc-stan.org/u/seantalts)\
**Post date:** [September 20, 2019, 10:05pm UTC](https://discourse.mc-stan.org/t/a-javascript-stanc3/11044/25 "2019-09-20T22:05:32Z")

</div>

> [@bgoodri](#):
>
> Maybe a better option would be for the class name to be the MD5sum of the Stan program?

+1 MD5 sum model name if obfuscated - better for caching with invalidation. The only thing stanc uses the model name for is printing error messages and the name of the C++ class, so if you are obfuscating it I’d send `stanc` the unobfuscated name still.

but wait, do you still need to obfuscate the name? You can just cache internally in `rstan::stanc` based on a MD5 sum of the model code, right? And you can call the resulting C++ file anything you want (you’ll have to write it out somewhere I suppose).

> [@bgoodri](#):
>
> `allow_undefined` tells `stanc` to not throw an error if you have a function that is declared but not defined, so that you can provide the definition in C++

Gotcha, I’ll work on that.

> [@bgoodri](#):
>
> `isystem` tells `stanc` what directories to look in for the C++ file that defines said functions

Wait, is this for finding Stan includes? Or C++ includes? Does this flag get passed to clang or to stanc?

The includes thing is tricky. The V8 thing has no access to the file system. You might have to read them in as well and pass them in as some kind of dictionary from file path to model string… :/

> [@bgoodri](#):
>
> Also, can the `resize_to_match` functions go inside the `namespace` ?

Yep.

> [@bgoodri](#):
>
> Finally, do we need a separate JavaScript file to call `stanfuncs` or can that be done with the existing one?

What is stanfuncs?

> [@bgoodri](#):
>
> Also, how do you get the messages if there is a syntax error in the Stan program, as in
> 
> ```no-highlight
> ctx$eval("stanc('mn', 'dat {}')")
> 
> ```
> 
> ?

Hm, it doesn’t seem to be propagating the exception nicely. I can wrap the return value in an object like this:

```no-highlight
{ cpp: "stan_model_cpp", warnings: ["warning1", "warning2"], errors: ["error1"] }

```

So if it succeeds, it will have a `cpp` key with a non-empty string. If it has warnings or errors they’ll be put in their lists. How’s that sound?

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [September 20, 2019, 10:15pm UTC](https://discourse.mc-stan.org/t/a-javascript-stanc3/11044/26 "2019-09-20T22:15:12Z")

</div>

> [@seantalts](#):
>
> Wait, is this for finding Stan includes? Or C++ includes? Does this flag get passed to clang or to stanc?

`stanc` for finding included Stan programs

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [September 20, 2019, 10:19pm UTC](https://discourse.mc-stan.org/t/a-javascript-stanc3/11044/27 "2019-09-20T22:19:55Z")

</div>

> [@seantalts](#):
>
> What is stanfuncs?

Ah, I guess it is called `compile_functions` in the Stan library  
[https://github.com/stan-dev/stan/blob/master/src/stan/lang/compile\_functions.hpp](https://github.com/stan-dev/stan/blob/master/src/stan/lang/compile_functions.hpp)

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [September 20, 2019, 10:20pm UTC](https://discourse.mc-stan.org/t/a-javascript-stanc3/11044/28 "2019-09-20T22:20:11Z")

</div>

> [@seantalts](#):
>
> So if it succeeds, it will have a `cpp` key with a non-empty string. If it has warnings or errors they’ll be put in their lists. How’s that sound?

Good

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [October 30, 2019, 6:54pm UTC](https://discourse.mc-stan.org/t/a-javascript-stanc3/11044/29 "2019-10-30T18:54:42Z")

</div>

I am trying to put this check into rstan but the C++ code

```
ctx <- V8::v8()
ctx$source("https://github.com/stan-dev/stanc3/releases/download/nightly/stanc.js")
ctx$eval("stanc('test', 'data {}')")

```

is generating is actually not valid. It has this declaration

```
class : public model_base_crtp<> {

```

which needs to be something like

```
class test
  : public stan::model::model_base_crtp<test> {

```

---

<div class="post-metadata">

**Author:** ![seantalts](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/seantalts/32/32_2.png) [@seantalts](https://discourse.mc-stan.org/u/seantalts)\
**Post date:** [October 30, 2019, 7:11pm UTC](https://discourse.mc-stan.org/t/a-javascript-stanc3/11044/30 "2019-10-30T19:11:38Z")

</div>

Sounds like a bug. I’ll take a look - I think we need to unify our IO a little more and not assume we have a file to work with in some places still.

---

<div class="post-metadata">

**Author:** ![seantalts](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/seantalts/32/32_2.png) [@seantalts](https://discourse.mc-stan.org/u/seantalts)\
**Post date:** [November 6, 2019, 11:36pm UTC](https://discourse.mc-stan.org/t/a-javascript-stanc3/11044/31 "2019-11-06T23:36:03Z")

</div>

The latest nightly should have this bug fixed. Still working on trying to figure out how to output JS objects and convert a bunch of our pipeline for errors and warnings that assumes we can just print to returning something instead…

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [November 6, 2019, 11:54pm UTC](https://discourse.mc-stan.org/t/a-javascript-stanc3/11044/32 "2019-11-06T23:54:50Z")

</div>

OK, that is ultimately necessary but not a huge priority for the next rstan release.

---

<div class="post-metadata">

**Author:** ![seantalts](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/seantalts/32/32_2.png) [@seantalts](https://discourse.mc-stan.org/u/seantalts)\
**Post date:** [November 27, 2019, 8:13pm UTC](https://discourse.mc-stan.org/t/a-javascript-stanc3/11044/33 "2019-11-27T20:13:56Z")

</div>

Woops, I thought I replied here already. The current nightly has the results wrapped in a js object now.

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [November 28, 2019, 4:58pm UTC](https://discourse.mc-stan.org/t/a-javascript-stanc3/11044/34 "2019-11-28T16:58:37Z")

</div>

Thanks. I will check it out.

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [November 28, 2019, 5:02pm UTC](https://discourse.mc-stan.org/t/a-javascript-stanc3/11044/35 "2019-11-28T17:02:24Z")

</div>

With the nightly, this

```
ctx <- V8::v8()
ctx$source("https://github.com/stan-dev/stanc3/releases/download/nightly/stanc.js")
ctx$eval("stanc('test', 'data {}')")

```

is just producing a string that says `"[object Object]"`. Am I supposed to be calling it differently now?

---

<div class="post-metadata">

**Author:** ![seantalts](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/seantalts/32/32_2.png) [@seantalts](https://discourse.mc-stan.org/u/seantalts)\
**Post date:** [November 28, 2019, 5:25pm UTC](https://discourse.mc-stan.org/t/a-javascript-stanc3/11044/36 "2019-11-28T17:25:53Z")

</div>

😂 let me take another look. Sorry about that.

---

<div class="post-metadata">

**Author:** ![seantalts](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/seantalts/32/32_2.png) [@seantalts](https://discourse.mc-stan.org/u/seantalts)\
**Post date:** [December 10, 2019, 12:51am UTC](https://discourse.mc-stan.org/t/a-javascript-stanc3/11044/37 "2019-12-10T00:51:35Z")

</div>

oh, try this instead:

```R
> ctx$call("stanc", "test", "data {}")

```

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [December 10, 2019, 2:45am UTC](https://discourse.mc-stan.org/t/a-javascript-stanc3/11044/38 "2019-12-10T02:45:44Z")

</div>

> [@seantalts](#):
>
> ctx$call(“stanc”, “test”, “data {}”)

OK. That works. Thanks.

[Previous page](https://discourse.mc-stan.org/t/a-javascript-stanc3/11044.md?page=1)
