Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
904354c
Update control.ts
DeveloperTryingToCodeLikeOtherOfThem Mar 22, 2026
4c06385
Update assert.md
DeveloperTryingToCodeLikeOtherOfThem Mar 22, 2026
92f04c3
Update assert.md
DeveloperTryingToCodeLikeOtherOfThem Mar 22, 2026
4a88612
Update control.ts
DeveloperTryingToCodeLikeOtherOfThem Mar 22, 2026
af26d39
Update control.ts
DeveloperTryingToCodeLikeOtherOfThem Mar 22, 2026
a78276c
Update control.ts
DeveloperTryingToCodeLikeOtherOfThem Mar 22, 2026
7107874
Update control.ts
DeveloperTryingToCodeLikeOtherOfThem Mar 22, 2026
98c0951
Update assert.md
DeveloperTryingToCodeLikeOtherOfThem Mar 22, 2026
9bfa97b
Update control.ts
DeveloperTryingToCodeLikeOtherOfThem Mar 22, 2026
27c7d64
Update control.ts
DeveloperTryingToCodeLikeOtherOfThem Mar 22, 2026
0a65bfb
Update control.ts
DeveloperTryingToCodeLikeOtherOfThem Mar 22, 2026
50c009a
Update control.ts
DeveloperTryingToCodeLikeOtherOfThem Mar 22, 2026
2efdecd
Update control.ts
DeveloperTryingToCodeLikeOtherOfThem Mar 22, 2026
3a8b174
Update control.ts
DeveloperTryingToCodeLikeOtherOfThem Mar 22, 2026
792bcc1
Update control.ts
DeveloperTryingToCodeLikeOtherOfThem Mar 22, 2026
f6f3574
Update control.ts
DeveloperTryingToCodeLikeOtherOfThem Mar 23, 2026
bb86d62
Update control.ts
DeveloperTryingToCodeLikeOtherOfThem Mar 23, 2026
bf250c2
Update assert.md
DeveloperTryingToCodeLikeOtherOfThem Mar 23, 2026
48fc1e0
Update assert.md
DeveloperTryingToCodeLikeOtherOfThem Mar 23, 2026
1d1eb95
Merge branch 'master' into patch-3
DeveloperTryingToCodeLikeOtherOfThem Apr 17, 2026
61e7722
Merge branch 'master' into patch-3
DeveloperTryingToCodeLikeOtherOfThem Apr 22, 2026
b862b83
Merge branch 'master' into patch-3
DeveloperTryingToCodeLikeOtherOfThem May 2, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions libs/base/control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ namespace control {

UNHANDLED_EXCEPTION = 999,
}

/**
* Display an error code and stop the program.
* @param code an error number to display. eg: 5
Expand All @@ -64,15 +65,28 @@ namespace control {
//% shim=pxtrt::panic
export function panic(code: number) { }

//% shim=U::userError
function _throwValue(msg: string) { }

/**
* Display an error code and stop the program when the assertion is `false`.
* Display an error message, an error code, and stop the program when the assertion is `false`.
@code (optional) an error number to display. eg: 5
*/
//% help=control/assert weight=30
//% blockId="control_assert" block="assert %cond|with value %code"
export function assert(cond: boolean, code: number) {
if (!cond) {
fail("Assertion failed, code=" + code)
}
export function assert(cond: boolean, code?: number) {
if (!cond) {
// adapted this idea from how real browsers work and optionally kept the code parameter for clearer diagnostics
// added the message also because the original one for receiving the message is only logged
// in the console, not together in the panel
const msg = code !== undefined
? `Assertion failed, code: ${code}`
: `Assertion failed`

console.log(msg)
dmesg(msg)
_throwValue(msg)
}
}

export function fail(message: string) {
Expand Down Expand Up @@ -158,14 +172,12 @@ namespace control {
export declare function programName(): string;

//% shim=control::_ramSize
function _ramSize() {
return 32 * 1024 * 1024;
}

declare function _ramSize(): number;

/** Returns estimated size of memory in bytes. */
export function ramSize() {
return getConfigValue(DAL.CFG_RAM_BYTES, 0) || _ramSize();
}
}

/** Runs the function and returns run time in microseconds. */
export function benchmark(f: () => void) {
Expand Down
10 changes: 5 additions & 5 deletions libs/base/docs/reference/control/assert.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# assert

Display an error number and stop the program if the assertion condition is false.
Display an error message, error number, and stop the program if the assertion condition is false.

```sig
control.assert(false, 0)
Expand All @@ -11,9 +11,9 @@ You can insist that your program will stop at an assert block if a certain condi
## Parameters

* **cond**: a [boolean](/types/boolean) where true means everything is ok or false which means, stop the program!
* **code**: an error [number](/types/number) you match to an error situation in your program.
* **code**: an optional parameter for displaying an error [number](/types/number) you match to an error situation in your program.

## Example #example
## Example

Stop the program if a sensor connected to pin `A0` sends a low (`0`) signal.

Expand All @@ -24,10 +24,10 @@ forever(function () {
})
```

## See also #seealso
## See also

[panic](/reference/control/panic)

```package
base
```
```