-
Notifications
You must be signed in to change notification settings - Fork 81
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
Exporting of IDL objects from js target. ts-js target added #575
Draft
lastmjs
wants to merge
1
commit into
dfinity:master
Choose a base branch
from
demergent-labs:export_javascript_idls
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -194,10 +194,13 @@ fn pp_defs<'a>( | |
env: &'a TypeEnv, | ||
def_list: &'a [&'a str], | ||
recs: &'a BTreeSet<&'a str>, | ||
export: bool, | ||
) -> RcDoc<'a> { | ||
let var_decl = if export { "export const" } else { "const" }; | ||
|
||
let recs_doc = lines( | ||
recs.iter() | ||
.map(|id| kwd("const").append(ident(id)).append(" = IDL.Rec();")), | ||
.map(|id| kwd(var_decl).append(ident(id)).append(" = IDL.Rec();")), | ||
); | ||
let defs = lines(def_list.iter().map(|id| { | ||
let ty = env.find_type(id).unwrap(); | ||
|
@@ -206,7 +209,7 @@ fn pp_defs<'a>( | |
.append(".fill") | ||
.append(enclose("(", pp_ty(ty), ");")) | ||
} else { | ||
kwd("const") | ||
kwd(var_decl) | ||
.append(ident(id)) | ||
.append(" = ") | ||
.append(pp_ty(ty)) | ||
|
@@ -231,35 +234,55 @@ fn pp_actor<'a>(ty: &'a Type, recs: &'a BTreeSet<&'a str>) -> RcDoc<'a> { | |
} | ||
} | ||
|
||
pub fn compile(env: &TypeEnv, actor: &Option<Type>) -> String { | ||
pub fn compile(env: &TypeEnv, actor: &Option<Type>, ts_js: bool) -> String { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's probably cleaner to have a new |
||
match actor { | ||
None => { | ||
let def_list: Vec<_> = env.0.iter().map(|pair| pair.0.as_ref()).collect(); | ||
let recs = infer_rec(env, &def_list).unwrap(); | ||
let doc = pp_defs(env, &def_list, &recs); | ||
let doc = pp_defs(env, &def_list, &recs, true); | ||
doc.pretty(LINE_WIDTH).to_string() | ||
} | ||
Some(actor) => { | ||
let def_list = chase_actor(env, actor).unwrap(); | ||
let recs = infer_rec(env, &def_list).unwrap(); | ||
let defs = pp_defs(env, &def_list, &recs); | ||
let defs_exported = pp_defs(env, &def_list, &recs, true); | ||
let defs_not_exported = pp_defs(env, &def_list, &recs, false); | ||
let init = if let TypeInner::Class(ref args, _) = actor.as_ref() { | ||
args.as_slice() | ||
} else { | ||
&[][..] | ||
}; | ||
let actor = kwd("return").append(pp_actor(actor, &recs)).append(";"); | ||
let body = defs.append(actor); | ||
let doc = str("export const idlFactory = ({ IDL }) => ") | ||
.append(enclose_space("{", body, "};")); | ||
let body = defs_not_exported.append(actor); | ||
let doc = if ts_js { | ||
str("") | ||
} else { | ||
str("import { IDL } from '@dfinity/candid';").append(RcDoc::line()) | ||
} | ||
.append(defs_exported) | ||
.append(format!( | ||
"export const idlFactory{} = ({{ IDL }}) => ", | ||
if ts_js { ": idlFactory" } else { "" } | ||
)) | ||
.append(enclose_space("{", body, "};")); | ||
// export init args | ||
let init_defs = chase_types(env, init).unwrap(); | ||
let init_defs = chase_types(env, init, None).unwrap(); | ||
let init_recs = infer_rec(env, &init_defs).unwrap(); | ||
let init_defs_doc = pp_defs(env, &init_defs, &init_recs); | ||
let init_defs_deduplicated = | ||
chase_types(env, init, Some(&BTreeSet::from_iter(def_list.clone()))).unwrap(); | ||
let init_recs_deduplicated = infer_rec(env, &init_defs_deduplicated).unwrap(); | ||
let init_defs_exported_doc = | ||
pp_defs(env, &init_defs_deduplicated, &init_recs_deduplicated, true); | ||
let init_defs_not_exported_doc = pp_defs(env, &init_defs, &init_recs, false); | ||
let init_doc = kwd("return").append(pp_args(init)).append(";"); | ||
let init_doc = init_defs_doc.append(init_doc); | ||
let init_doc = | ||
str("export const init = ({ IDL }) => ").append(enclose_space("{", init_doc, "};")); | ||
let init_doc = init_defs_not_exported_doc.append(init_doc); | ||
let init_doc = init_defs_exported_doc | ||
.append(format!( | ||
"export const init{} = ({}) => ", | ||
if ts_js { ": init" } else { "" }, | ||
if init_defs.len() != 0 { "{ IDL }" } else { "" } | ||
)) | ||
.append(enclose_space("{", init_doc, "};")); | ||
let init_doc = init_doc.pretty(LINE_WIDTH).to_string(); | ||
let doc = doc.append(RcDoc::hardline()).append(init_doc); | ||
doc.pretty(LINE_WIDTH).to_string() | ||
|
@@ -405,7 +428,7 @@ import { Principal } from './principal'; | |
}, | ||
) | ||
.unwrap(); | ||
res += &super::compile(&env, &None); | ||
res += &super::compile(&env, &None, false); | ||
for (i, assert) in test.asserts.iter().enumerate() { | ||
let mut types = Vec::new(); | ||
for ty in assert.typ.iter() { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ pub mod javascript; | |
pub mod motoko; | ||
pub mod rust; | ||
pub mod typescript; | ||
pub mod typescript_and_javascript; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
rust/candid_parser/src/bindings/typescript_and_javascript.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use super::javascript::compile as javascript_compile; | ||
use super::typescript::compile as typescript_compile; | ||
use candid::pretty::utils::*; | ||
use candid::types::{Type, TypeEnv}; | ||
|
||
pub fn compile(env: &TypeEnv, actor: &Option<Type>) -> String { | ||
let ts = typescript_compile(env, actor, true); | ||
let js = javascript_compile(env, actor, true); | ||
|
||
str(&ts.clone()).append(js).pretty(LINE_WIDTH).to_string() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's make it a new function, so that we don't break existing code. It only needs
pub(crate)
visibility, I think.