Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please explain ‘use’ #62

Open
bentxt opened this issue May 9, 2024 · 1 comment
Open

Please explain ‘use’ #62

bentxt opened this issue May 9, 2024 · 1 comment
Labels
question Further information is requested

Comments

@bentxt
Copy link

bentxt commented May 9, 2024

Hi

could you explain how ‘use’ works?

https://github.com/udem-dlteam/ribbit/blob/dev/src/host/js/lib/prim-io.scm


(define-primitive
  (##stdout-fd)
  (use js/node/fs)
  "() => push(1),")

`

@leo-ard
Copy link
Collaborator

leo-ard commented May 9, 2024

Hi !

The use clause describe relationships between "features". In Ribbit, a feature describes some piece of code that needs to be added inside the output of the compiler (a specialized rvm with the embedded bytecode). This can take the form of adding a primitive into the primitive table, an importation to a needed package or the definition of a conversion function. The example you have here is quite confusing because the feature "js/node/fs" is actually not needed for this primitive (it's a bug). Here is a better example to understand the use clause :

(define-primitive 
  (##get-fd-input-file filename)
  (use js/node/fs scm2str)
  "prim1(filename => {try{return fs.openSync(scm2str(filename), 'r')}catch{return FALSE}}),")

This feature uses two features js/node/fs and scm2str. The first feature refers to this code inside the rvm.js file :

fs = require("fs"); // @@(feature (or js/node/fs ##getchar ##putchar))@@

Here, a comment between @@( and )@@ tells the compiler that the line fs = require("fs") must be included only if the feature js/node/fs is activated. Note that it will also be included if the features "##getchar" or "##putchar" is activated.

The second feature refers to this par of the code inside the rvm.js:

// @@(feature scm2str
scm2str = (r) => {
    let f = (c) => (c===NIL?"":String.fromCharCode(c[0])+f(c[1]));
    return f(r[0]);
};
// )@@

This defines a conversion between scheme (Ribbit) strings and JavaScript strings.

If we look back at the primitive we originally had, we see that the primitive uses (inside the string) the fs module and the conversion function. The use clause tells the compiler to include the two pieces of code inside the rvm when generating the code. This is needed by the primitive because without these pieces of code, the primitive cannot work properly. If you want more info about this, we have a paper on it. The use clause is discussed in section 4 : https://www.iro.umontreal.ca/~feeley/papers/OLearyFeeleyMOREVMS23.pdf

If you want to fix the prim-io file of the js host by removing the unnecessary use clauses, I'll accept the PR right away ! I think some of the primitives inside this file are affected. Don't forget to target the PR to the dev branch if you do it.

@leo-ard leo-ard added the question Further information is requested label May 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants