Skip to content

Commit 0228b47

Browse files
authored
feat: add primitive predicate isReplicatedExecution (#4929)
1 parent 7c2b861 commit 0228b47

11 files changed

+36
-1
lines changed

Changelog.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Motoko compiler changelog
22

3+
## 0.14.3 (FUTURE)
4+
5+
* motoko (`moc`)
6+
7+
* Added primitive predicate `isReplicatedExecution` (#4929).
8+
39
## 0.14.2 (2025-02-26)
410

511
* motoko (`moc`)

src/codegen/compile_classical.ml

+6
Original file line numberDiff line numberDiff line change
@@ -5068,6 +5068,7 @@ module IC = struct
50685068
E.add_func_import env "ic0" "canister_self_size" [] [I32Type];
50695069
E.add_func_import env "ic0" "canister_status" [] [I32Type];
50705070
E.add_func_import env "ic0" "canister_version" [] [I64Type];
5071+
E.add_func_import env "ic0" "in_replicated_execution" [] [I32Type];
50715072
E.add_func_import env "ic0" "is_controller" (i32s 2) [I32Type];
50725073
E.add_func_import env "ic0" "debug_print" (i32s 2) [];
50735074
E.add_func_import env "ic0" "msg_arg_data_copy" (i32s 3) [];
@@ -5182,6 +5183,7 @@ module IC = struct
51825183

51835184
let performance_counter = ic_system_call "performance_counter"
51845185
let is_controller = ic_system_call "is_controller"
5186+
let replicated_execution = ic_system_call "in_replicated_execution"
51855187
let canister_version = ic_system_call "canister_version"
51865188

51875189
let print_ptr_len env = G.i (Call (nr (E.built_in env "print_ptr")))
@@ -11772,6 +11774,10 @@ and compile_prim_invocation (env : E.t) ae p es at =
1177211774
Blob.len env ^^
1177311775
IC.is_controller env
1177411776

11777+
| OtherPrim "replicated_execution", [] ->
11778+
SR.Vanilla,
11779+
IC.replicated_execution env
11780+
1177511781
| OtherPrim "canister_version", [] ->
1177611782
SR.UnboxedWord64 Type.Nat64,
1177711783
IC.canister_version env

src/codegen/compile_enhanced.ml

+9
Original file line numberDiff line numberDiff line change
@@ -4725,6 +4725,7 @@ module IC = struct
47254725
E.add_func_import env "ic0" "canister_self_size" [] [I64Type];
47264726
E.add_func_import env "ic0" "canister_status" [] [I32Type];
47274727
E.add_func_import env "ic0" "canister_version" [] [I64Type];
4728+
E.add_func_import env "ic0" "in_replicated_execution" [] [I32Type];
47284729
E.add_func_import env "ic0" "is_controller" (i64s 2) [I32Type];
47294730
E.add_func_import env "ic0" "debug_print" (i64s 2) [];
47304731
E.add_func_import env "ic0" "msg_arg_data_copy" (i64s 3) [];
@@ -4871,6 +4872,10 @@ module IC = struct
48714872
ic_system_call "is_controller" env ^^
48724873
G.i (Convert (Wasm_exts.Values.I64 I64Op.ExtendUI32))
48734874

4875+
let replicated_execution env =
4876+
ic_system_call "in_replicated_execution" env ^^
4877+
G.i (Convert (Wasm_exts.Values.I64 I64Op.ExtendUI32))
4878+
48744879
let canister_version env = ic_system_call "canister_version" env
48754880

48764881
let print_ptr_len env = G.i (Call (nr (E.built_in env "print_ptr")))
@@ -11869,6 +11874,10 @@ and compile_prim_invocation (env : E.t) ae p es at =
1186911874
Blob.len env ^^
1187011875
IC.is_controller env
1187111876

11877+
| OtherPrim "replicated_execution", [] ->
11878+
SR.Vanilla,
11879+
IC.replicated_execution env
11880+
1187211881
| OtherPrim "canister_version", [] ->
1187311882
SR.UnboxedWord64 Type.Nat64,
1187411883
IC.canister_version env

src/ir_def/construct.ml

+2-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ let primE prim es =
118118
| OtherPrim "array_len"
119119
| OtherPrim "blob_size"
120120
| OtherPrim "text_len" -> T.nat
121-
| OtherPrim "is_controller" -> T.bool
121+
| OtherPrim "is_controller"
122+
| OtherPrim "replicated_execution" -> T.bool
122123
| OtherPrim "rts_version" -> T.text
123124
| OtherPrim "rts_memory_size" -> T.nat
124125
| OtherPrim "rts_heap_size" -> T.nat

src/prelude/prim.mo

+1
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ func principalOfBlob(act : Blob) : Principal {
326326

327327
func principalOfActor(act : actor {}) : Principal = (prim "principalOfActor" : (actor {}) -> Principal) act;
328328
func isController(p : Principal) : Bool = (prim "is_controller" : Principal -> Bool) p;
329+
func isReplicatedExecution() : Bool = (prim "replicated_execution" : () -> Bool) ();
329330
func canisterVersion() : Nat64 = (prim "canister_version" : () -> Nat64)();
330331

331332
// Untyped dynamic actor creation from blobs

test/fail/ok/no-timer-canc.tc.ok

+1
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ no-timer-canc.mo:3.10-3.21: type error [M0119], object field cancelTimer is not
150150
intToNat64Wrap : Int -> Nat64;
151151
intToNat8Wrap : Int -> Nat8;
152152
isController : Principal -> Bool;
153+
isReplicatedExecution : () -> Bool;
153154
log : Float -> Float;
154155
nat16ToInt16 : Nat16 -> Int16;
155156
nat16ToNat : Nat16 -> Nat;

test/fail/ok/no-timer-set.tc.ok

+1
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ no-timer-set.mo:3.10-3.18: type error [M0119], object field setTimer is not cont
150150
intToNat64Wrap : Int -> Nat64;
151151
intToNat8Wrap : Int -> Nat8;
152152
isController : Principal -> Bool;
153+
isReplicatedExecution : () -> Bool;
153154
log : Float -> Float;
154155
nat16ToInt16 : Nat16 -> Int16;
155156
nat16ToNat : Nat16 -> Nat;

test/fail/ok/suggest-long-ai.tc.ok

+1
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ suggest-long-ai.mo:4.6-4.13: type error [M0072], field stableM does not exist in
151151
intToNat64Wrap : Int -> Nat64;
152152
intToNat8Wrap : Int -> Nat8;
153153
isController : Principal -> Bool;
154+
isReplicatedExecution : () -> Bool;
154155
log : Float -> Float;
155156
nat16ToInt16 : Nat16 -> Int16;
156157
nat16ToNat : Nat16 -> Nat;

test/fail/ok/suggest-short-ai.tc.ok

+1
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ suggest-short-ai.mo:4.6-4.7: type error [M0072], field s does not exist in type:
151151
intToNat64Wrap : Int -> Nat64;
152152
intToNat8Wrap : Int -> Nat8;
153153
isController : Principal -> Bool;
154+
isReplicatedExecution : () -> Bool;
154155
log : Float -> Float;
155156
nat16ToInt16 : Nat16 -> Int16;
156157
nat16ToNat : Nat16 -> Nat;

test/run-drun/ok/query.drun-run.ok

+1
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ debug.print: 5
1717
Ok: Reply: 0x4449444c00017d04
1818
debug.print: 4
1919
ingress Completed: Reply: 0x4449444c0000
20+
ingress Completed: Reply: 0x4449444c00017d04

test/run-drun/query.mo

+7
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,15 @@ actor {
99
Prim.debugPrintNat c
1010
};
1111
public func get() : async Nat {
12+
assert Prim.isReplicatedExecution();
13+
return c
14+
};
15+
public query func getq() : async Nat {
16+
assert Prim.isReplicatedExecution();
1217
return c
1318
};
1419
public query func read() : async Nat {
20+
assert not (Prim.isReplicatedExecution());
1521
let tmp = c;
1622
c += 1;
1723
Prim.debugPrintNat c;
@@ -28,6 +34,7 @@ actor {
2834
//CALL ingress printCounter 0x4449444C0000
2935
//CALL query read 0x4449444C0000
3036
//CALL ingress printCounter 0x4449444C0000
37+
//CALL ingress getq 0x4449444C0000
3138

3239
//SKIP run
3340
//SKIP run-ir

0 commit comments

Comments
 (0)