Skip to content

Commit

Permalink
Merge pull request #1332 from demergent-labs/js_refactor
Browse files Browse the repository at this point in the history
add type checking for refactored code
  • Loading branch information
lastmjs authored Oct 4, 2023
2 parents 605103c + 2d8d1a1 commit 21283fc
Show file tree
Hide file tree
Showing 85 changed files with 882 additions and 933 deletions.
4 changes: 2 additions & 2 deletions examples/cycles/src/cycles/index.did
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
service: () -> {
receiveCycles: () -> (nat64);
receiveCycles128: () -> (nat);
getCanisterBalance: () -> (nat64) query;
getCanisterBalance128: () -> (nat) query;
receiveCycles: () -> (nat64);
receiveCycles128: () -> (nat);
}
6 changes: 3 additions & 3 deletions examples/cycles/src/intermediary/index.did
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
service: () -> {
getCanisterBalance: () -> (nat64) query;
getCanisterBalance128: () -> (nat) query;
sendCycles: () -> (nat64);
sendCyclesNotify: () -> ();
sendCycles128: () -> (nat);
sendCycles128Notify: () -> ();
getCanisterBalance: () -> (nat64) query;
getCanisterBalance128: () -> (nat) query;
sendCyclesNotify: () -> ();
}
10 changes: 5 additions & 5 deletions examples/func_types/canisters/func_types/index.did
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
type rec_26 = func (record {id:text; complexFunc:rec_26; basicFunc:func (text) -> (text) query}, variant {Bad; ComplexFunc:rec_26; Good; BasicFunc:func (text) -> (text) query}) -> (nat64);
type rec_29 = func (record {id:text; complexFunc:rec_29; basicFunc:func (text) -> (text) query}, variant {Bad; ComplexFunc:rec_29; Good; BasicFunc:func (text) -> (text) query}) -> (nat64);
type rec_34 = func (record {id:text; complexFunc:rec_34; basicFunc:func (text) -> (text) query}, variant {Bad; ComplexFunc:rec_34; Good; BasicFunc:func (text) -> (text) query}) -> (nat64);
type rec_0 = func (record {id:text; complexFunc:rec_0; basicFunc:func (text) -> (text) query}, variant {Bad; ComplexFunc:rec_0; Good; BasicFunc:func (text) -> (text) query}) -> (nat64);
type rec_1 = func (record {id:text; complexFunc:rec_1; basicFunc:func (text) -> (text) query}, variant {Bad; ComplexFunc:rec_1; Good; BasicFunc:func (text) -> (text) query}) -> (nat64);
type rec_2 = func (record {id:text; complexFunc:rec_2; basicFunc:func (text) -> (text) query}, variant {Bad; ComplexFunc:rec_2; Good; BasicFunc:func (text) -> (text) query}) -> (nat64);
service: () -> {
basicFuncParam: (func (text) -> (text) query) -> (func (text) -> (text) query) query;
basicFuncParamArray: (vec func (text) -> (text) query) -> (vec func (text) -> (text) query) query;
basicFuncReturnType: () -> (func (text) -> (text) query) query;
basicFuncReturnTypeArray: () -> (vec func (text) -> (text) query) query;
complexFuncParam: (rec_26) -> (rec_29) query;
complexFuncReturnType: () -> (rec_34) query;
complexFuncParam: (rec_0) -> (rec_1) query;
complexFuncReturnType: () -> (rec_2) query;
getNotifierFromNotifiersCanister: () -> (func (vec nat8) -> () oneway);
getStableFunc: () -> (func (nat64, text) -> () query) query;
nullFuncParam: (func (opt null, vec null, null, vec vec null, vec opt null) -> (null) query) -> (func (opt null, vec null, null, vec vec null, vec opt null) -> (null) query) query;
Expand Down
44 changes: 15 additions & 29 deletions examples/manual_reply/src/index.did
Original file line number Diff line number Diff line change
@@ -1,39 +1,25 @@
type rec_1 = record {electrons:nat8; layer:nat8};
type rec_3 = variant {Elemental; Mixed; Toxic};
type rec_4 = record {element:text};
type rec_2 = variant {Gas:rec_3; Solid:rec_4; Liquid};
type rec_0 = record {id:text; orbitals:vec rec_1; state:rec_2};
type rec_5 = variant {Elemental; Mixed; Toxic};
type rec_7 = variant {Large; Small; Medium};
type rec_6 = record {int:int; bool:bool; text:text; myBlob:vec nat8; myVariant:rec_7};
type rec_9 = record {electrons:nat8; layer:nat8};
type rec_11 = variant {Elemental; Mixed; Toxic};
type rec_12 = record {element:text};
type rec_10 = variant {Gas:rec_11; Solid:rec_12; Liquid};
type rec_8 = record {id:text; orbitals:vec rec_9; state:rec_10};
type rec_13 = variant {Elemental; Mixed; Toxic};
service: () -> {
manualUpdate: (text) -> (text);
updateBlob: () -> (vec nat8);
updateFloat32: () -> (float32);
updateInt8: () -> (int8);
updateNat: () -> (nat);
updateNull: () -> (null);
updateVoid: () -> ();
updateRecord: () -> (rec_0);
updateReserved: () -> (reserved);
updateString: () -> (text);
updateVariant: () -> (rec_5);
replyRaw: () -> (rec_6);
manualQuery: (text) -> (text) query;
manualUpdate: (text) -> (text);
queryBlob: () -> (vec nat8) query;
queryFloat32: () -> (float32) query;
queryInt8: () -> (int8) query;
queryNat: () -> (nat) query;
queryNull: () -> (null) query;
queryVoid: () -> () query;
queryRecord: () -> (rec_8) query;
queryRecord: () -> (record {id:text; orbitals:vec record {electrons:nat8; layer:nat8}; state:variant {Gas:variant {Elemental; Mixed; Toxic}; Solid:record {element:text}; Liquid}}) query;
queryReserved: () -> (reserved) query;
queryString: () -> (text) query;
queryVariant: () -> (rec_13) query;
queryVariant: () -> (variant {Elemental; Mixed; Toxic}) query;
queryVoid: () -> () query;
replyRaw: () -> (record {int:int; bool:bool; text:text; myBlob:vec nat8; myVariant:variant {Large; Small; Medium}});
updateBlob: () -> (vec nat8);
updateFloat32: () -> (float32);
updateInt8: () -> (int8);
updateNat: () -> (nat);
updateNull: () -> (null);
updateRecord: () -> (record {id:text; orbitals:vec record {electrons:nat8; layer:nat8}; state:variant {Gas:variant {Elemental; Mixed; Toxic}; Solid:record {element:text}; Liquid}});
updateReserved: () -> (reserved);
updateString: () -> (text);
updateVariant: () -> (variant {Elemental; Mixed; Toxic});
updateVoid: () -> ();
}
2 changes: 1 addition & 1 deletion examples/manual_reply/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export default Canister({
),
updateVoid: update(
[],
Void,
Manual(Void),
() => {
ic.reply(undefined, Void);
},
Expand Down
2 changes: 1 addition & 1 deletion examples/motoko_examples/persistent-storage/src/index.did
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
service: () -> {
get: () -> (nat) query;
getRedeployed: () -> (bool) query;
increment: () -> (nat);
get: () -> (nat) query;
reset: () -> (nat);
}
102 changes: 51 additions & 51 deletions examples/recursion/src/recursion/index.did
Original file line number Diff line number Diff line change
@@ -1,53 +1,53 @@
type rec_365 = func (rec_365) -> (rec_365) query;
type rec_368 = func (rec_368) -> (rec_368) query;
type rec_373 = func (rec_373) -> (rec_373) query;
type rec_281 = record {myOpt:opt rec_281};
type rec_284 = record {myOpt:opt rec_284};
type rec_329 = record {myOpt:opt rec_329};
type rec_297 = record {myVar:variant {num:int8; varRec:rec_297}};
type rec_300 = record {myVar:variant {num:int8; varRec:rec_300}};
type rec_337 = record {myVar:variant {num:int8; varRec:rec_337}};
type rec_289 = record {myVecRecords:vec rec_289};
type rec_292 = record {myVecRecords:vec rec_292};
type rec_333 = record {myVecRecords:vec rec_333};
type rec_385 = service {getMessage: () -> (text) query; myQuery: (rec_385) -> (rec_385) query;};
type rec_388 = service {getMessage: () -> (text) query; myQuery: (rec_388) -> (rec_388) query;};
type rec_397 = service {getMessage: () -> (text) query; myQuery: (rec_397) -> (rec_397) query;};
type rec_400 = service {getMessage: () -> (text) query; myQuery: (rec_400) -> (rec_400) query;};
type rec_393 = service {getMessage: () -> (text) query; myQuery: (rec_393) -> (rec_393) query;};
type rec_377 = service {myQuery: (rec_377) -> (rec_377) query;};
type rec_380 = service {myQuery: (rec_380) -> (rec_380) query;};
type rec_313 = record {opt rec_313; opt rec_313};
type rec_316 = record {opt rec_316; opt rec_316};
type rec_345 = record {opt rec_345; opt rec_345};
type rec_353 = record {variant {num:int8; varTuple:rec_353}; variant {num:int8; varTuple:rec_353}};
type rec_356 = record {variant {num:int8; varTuple:rec_356}; variant {num:int8; varTuple:rec_356}};
type rec_361 = record {variant {num:int8; varTuple:rec_361}; variant {num:int8; varTuple:rec_361}};
type rec_321 = record {vec rec_321; vec rec_321};
type rec_324 = record {vec rec_324; vec rec_324};
type rec_349 = record {vec rec_349; vec rec_349};
type rec_305 = variant {num:int8; recVariant:rec_305};
type rec_308 = variant {num:int8; recVariant:rec_308};
type rec_341 = variant {num:int8; recVariant:rec_341};
type rec_21 = func (rec_21) -> (rec_21) query;
type rec_22 = func (rec_22) -> (rec_22) query;
type rec_23 = func (rec_23) -> (rec_23) query;
type rec_0 = record {myOpt:opt rec_0};
type rec_1 = record {myOpt:opt rec_1};
type rec_12 = record {myOpt:opt rec_12};
type rec_4 = record {myVar:variant {num:int8; varRec:rec_4}};
type rec_5 = record {myVar:variant {num:int8; varRec:rec_5}};
type rec_14 = record {myVar:variant {num:int8; varRec:rec_14}};
type rec_2 = record {myVecRecords:vec rec_2};
type rec_3 = record {myVecRecords:vec rec_3};
type rec_13 = record {myVecRecords:vec rec_13};
type rec_26 = service {getMessage: () -> (text) query; myQuery: (rec_26) -> (rec_26) query;};
type rec_27 = service {getMessage: () -> (text) query; myQuery: (rec_27) -> (rec_27) query;};
type rec_29 = service {getMessage: () -> (text) query; myQuery: (rec_29) -> (rec_29) query;};
type rec_30 = service {getMessage: () -> (text) query; myQuery: (rec_30) -> (rec_30) query;};
type rec_28 = service {getMessage: () -> (text) query; myQuery: (rec_28) -> (rec_28) query;};
type rec_24 = service {myQuery: (rec_24) -> (rec_24) query;};
type rec_25 = service {myQuery: (rec_25) -> (rec_25) query;};
type rec_8 = record {opt rec_8; opt rec_8};
type rec_9 = record {opt rec_9; opt rec_9};
type rec_16 = record {opt rec_16; opt rec_16};
type rec_18 = record {variant {num:int8; varTuple:rec_18}; variant {num:int8; varTuple:rec_18}};
type rec_19 = record {variant {num:int8; varTuple:rec_19}; variant {num:int8; varTuple:rec_19}};
type rec_20 = record {variant {num:int8; varTuple:rec_20}; variant {num:int8; varTuple:rec_20}};
type rec_10 = record {vec rec_10; vec rec_10};
type rec_11 = record {vec rec_11; vec rec_11};
type rec_17 = record {vec rec_17; vec rec_17};
type rec_6 = variant {num:int8; recVariant:rec_6};
type rec_7 = variant {num:int8; recVariant:rec_7};
type rec_15 = variant {num:int8; recVariant:rec_15};
service: () -> {
testRecFunc: (rec_365) -> (rec_368) query;
testRecFuncReturn: () -> (rec_373) query;
testRecRecordWithOpt: (rec_281) -> (rec_284) query;
testRecRecordWithOptReturn: () -> (rec_329) query;
testRecRecordWithVariant: (rec_297) -> (rec_300) query;
testRecRecordWithVariantReturn: () -> (rec_337) query;
testRecRecordWithVec: (rec_289) -> (rec_292) query;
testRecRecordWithVecReturn: () -> (rec_333) query;
testRecService: (rec_385) -> (rec_388) query;
testRecServiceCall: (rec_397) -> (rec_400);
testRecServiceReturn: () -> (rec_393) query;
testRecServiceSimple: (rec_377) -> (rec_380) query;
testRecTupleWithOpt: (rec_313) -> (rec_316) query;
testRecTupleWithOptReturn: () -> (rec_345) query;
testRecTupleWithVariant: (rec_353) -> (rec_356) query;
testRecTupleWithVariantReturn: () -> (rec_361) query;
testRecTupleWithVec: (rec_321) -> (rec_324) query;
testRecTupleWithVecReturn: () -> (rec_349) query;
testRecVariant: (rec_305) -> (rec_308) query;
testRecVariantReturn: () -> (rec_341) query;
testRecFunc: (rec_21) -> (rec_22) query;
testRecFuncReturn: () -> (rec_23) query;
testRecRecordWithOpt: (rec_0) -> (rec_1) query;
testRecRecordWithOptReturn: () -> (rec_12) query;
testRecRecordWithVariant: (rec_4) -> (rec_5) query;
testRecRecordWithVariantReturn: () -> (rec_14) query;
testRecRecordWithVec: (rec_2) -> (rec_3) query;
testRecRecordWithVecReturn: () -> (rec_13) query;
testRecService: (rec_26) -> (rec_27) query;
testRecServiceCall: (rec_29) -> (rec_30);
testRecServiceReturn: () -> (rec_28) query;
testRecServiceSimple: (rec_24) -> (rec_25) query;
testRecTupleWithOpt: (rec_8) -> (rec_9) query;
testRecTupleWithOptReturn: () -> (rec_16) query;
testRecTupleWithVariant: (rec_18) -> (rec_19) query;
testRecTupleWithVariantReturn: () -> (rec_20) query;
testRecTupleWithVec: (rec_10) -> (rec_11) query;
testRecTupleWithVecReturn: () -> (rec_17) query;
testRecVariant: (rec_6) -> (rec_7) query;
testRecVariantReturn: () -> (rec_15) query;
}
6 changes: 3 additions & 3 deletions examples/recursion/src/recursive_canister/index.did
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
type rec_18 = service {getMessage: () -> (text) query; myQuery: (rec_18) -> (rec_18) query;};
type rec_21 = service {getMessage: () -> (text) query; myQuery: (rec_21) -> (rec_21) query;};
type rec_0 = service {getMessage: () -> (text) query; myQuery: (rec_0) -> (rec_0) query;};
type rec_1 = service {getMessage: () -> (text) query; myQuery: (rec_1) -> (rec_1) query;};
service: (text) -> {
getMessage: () -> (text) query;
myQuery: (rec_18) -> (rec_21) query;
myQuery: (rec_0) -> (rec_1) query;
}
17 changes: 1 addition & 16 deletions src/compiler/compile_typescript_code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,8 @@ export function compileTypeScriptToJavaScript(
${imports}
`);

const javaScriptCodeWithRandom = `
globalThis.crypto = {
getRandomValues: () => {
let array = new Uint8Array(32);
for (let i = 0; i < array.length; i++) {
array[i] = Math.floor(Math.random() * 256);
}
return array;
}
};
${bundledJavaScript}
`;

return {
ok: javaScriptCodeWithRandom
ok: bundledJavaScript
};
} catch (err) {
return { err };
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/rust/canister/src/ic/call_raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,13 @@ pub fn native_function<'a>(

if should_resolve {
let resolve = global
.get_property("_azleResolveIds").unwrap()
.get_property(format!("_resolve_{promise_id}").as_str())
.unwrap();
resolve.call(&resolve, &[js_value_ref]).unwrap();
} else {
let reject = global
.get_property("_azleRejectIds").unwrap()
.get_property(format!("_reject_{promise_id}").as_str())
.unwrap();
reject.call(&reject, &[js_value_ref]).unwrap();
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/rust/canister/src/ic/call_raw128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,13 @@ pub fn native_function<'a>(

if should_resolve {
let resolve = global
.get_property("_azleResolveIds").unwrap()
.get_property(format!("_resolve_{promise_id}").as_str())
.unwrap();
resolve.call(&resolve, &[js_value_ref]).unwrap();
} else {
let reject = global
.get_property("_azleRejectIds").unwrap()
.get_property(format!("_reject_{promise_id}").as_str())
.unwrap();
reject.call(&reject, &[js_value_ref]).unwrap();
Expand Down
1 change: 1 addition & 0 deletions src/compiler/rust/canister/src/ic/set_timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub fn native_function<'a>(
let global = context.global_object().unwrap();

let timer_callback = global
.get_property("_azleTimerCallbackIds").unwrap()
.get_property(callback_id.as_str())
.unwrap_or_else(|e| ic_cdk::api::trap(e.to_string().as_str()));

Expand Down
1 change: 1 addition & 0 deletions src/compiler/rust/canister/src/ic/set_timer_interval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub fn native_function<'a>(
let global = context.global_object().unwrap();

let timer_callback = global
.get_property("_azleTimerCallbackIds").unwrap()
.get_property(callback_id.as_str())
.unwrap_or_else(|e| ic_cdk::api::trap(e.to_string().as_str()));

Expand Down
23 changes: 8 additions & 15 deletions src/lib/candid/did_visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export class DidVisitor extends IDL.Visitor<VisitorData, VisitorResult> {
data: VisitorData
): VisitorResult {
const fields = components.map((value) =>
hch(value).accept(this, { ...data, isOnService: false })
value.accept(this, { ...data, isOnService: false })
);
const candid = extractCandid(fields);
return [`record {${candid[0].join('; ')}}`, candid[1]];
Expand All @@ -140,24 +140,24 @@ export class DidVisitor extends IDL.Visitor<VisitorData, VisitorResult> {
ty: IDL.Type<T>,
data: VisitorData
): VisitorResult {
const candid = hch(ty).accept(this, { ...data, isOnService: false });
const candid = ty.accept(this, { ...data, isOnService: false });
return [`opt ${candid[0]}`, candid[1]];
}
visitVec<T>(
t: IDL.VecClass<T>,
ty: IDL.Type<T>,
data: VisitorData
): VisitorResult {
const candid = hch(ty).accept(this, { ...data, isOnService: false });
const candid = ty.accept(this, { ...data, isOnService: false });
return [`vec ${candid[0]}`, candid[1]];
}
visitFunc(t: IDL.FuncClass, data: VisitorData): VisitorResult {
const argsTypes = t.argTypes.map((value) =>
hch(value).accept(this, { ...data, isOnService: false })
value.accept(this, { ...data, isOnService: false })
);
const candidArgs = extractCandid(argsTypes);
const retsTypes = t.retTypes.map((value) =>
hch(value).accept(this, { ...data, isOnService: false })
value.accept(this, { ...data, isOnService: false })
);
const candidRets = extractCandid(retsTypes);
const args = candidArgs[0].join(', ');
Expand All @@ -180,7 +180,7 @@ export class DidVisitor extends IDL.Visitor<VisitorData, VisitorResult> {
// Everything else will just be the normal inline candid def
const usedRecClasses = data.usedRecClasses;
if (!usedRecClasses.includes(t)) {
const candid = hch(ty).accept(this, {
const candid = ty.accept(this, {
...data,
usedRecClasses: [...usedRecClasses, t],
isOnService: false,
Expand All @@ -198,7 +198,7 @@ export class DidVisitor extends IDL.Visitor<VisitorData, VisitorResult> {
data: VisitorData
): VisitorResult {
const candidFields = fields.map(([key, value]) =>
hch(value).accept(this, { ...data, isOnService: false })
value.accept(this, { ...data, isOnService: false })
);
const candid = extractCandid(candidFields);
const field_strings = fields.map(
Expand All @@ -212,7 +212,7 @@ export class DidVisitor extends IDL.Visitor<VisitorData, VisitorResult> {
data: VisitorData
): VisitorResult {
const candidFields = fields.map(([key, value]) =>
hch(value).accept(this, { ...data, isOnService: false })
value.accept(this, { ...data, isOnService: false })
);
const candid = extractCandid(candidFields);
const fields_string = fields.map(
Expand Down Expand Up @@ -249,10 +249,3 @@ function extractCandid(
);
return [paramCandid, candidTypeDefs];
}

function hch(value: any) {
if (value._azleIsCanister) {
return value().getIDL();
}
return value;
}
Loading

0 comments on commit 21283fc

Please sign in to comment.