Skip to content

Commit

Permalink
jogs:0.2.0 (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
Enter-tainer authored Nov 7, 2023
1 parent f7ff5b2 commit 3951a1d
Show file tree
Hide file tree
Showing 7 changed files with 532 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/preview/jogs/0.2.0/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 mgt

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
130 changes: 130 additions & 0 deletions packages/preview/jogs/0.2.0/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# Jogs

Quickjs javascript runtime for typst. This package provides a typst plugin for evaluating javascript code.

## Example

````typst
#import "@preview/jogs:0.2.0": *
#set page(height: auto, width: auto, fill: black, margin: 1em)
#set text(fill: white)
#let code = ```
function sum() {
const total = Array.prototype.slice.call(arguments).reduce(function(a, b) {
return a + b;
}, 0);
return total;
}
function string_join(arr) {
return arr.join(" | ");
}
function return_complex_obj() {
return {
a: 1,
b: "2",
c: [1, 2, 3],
d: {
e: 1,
},
};
}
```
#let bytecode = compile-js(code)
#list-global-property(bytecode)
#call-js-function(bytecode, "sum", 6, 7, 8, 9, 10)
#call-js-function(bytecode, "string_join", ("a", "b", "c", "d", "e"),)
#call-js-function(bytecode, "return_complex_obj",)
````

result:

![](typst-package/examples/fib.svg)

## Documentation

This package provide following function(s):

### `eval-js`

Run a Javascript code snippet.

#### Arguments
* `code` - The Javascript code to run. It can be a string or a raw block.

#### Returns
The result of the Javascript code. The type is the typst type which most closely resembles the Javascript type.

#### Example

```typ
#let result = eval-js("1 + 1")
```

### `compile-js`

Compile a Javascript code snippet into bytecode.

#### Arguments

* `code` - The Javascript code to compile. It can be a string or a raw block.

#### Returns

The bytecode of the Javascript code. The type is `bytes`.

#### Example

```typ
#let bytecode = compile-js("function sum(a, b) { return a + b; }")
```

### `call-js-function`

Call a Javascript function with arguments.

#### Arguments

* `bytecode` - The bytecode of the Javascript function. It can be obtained by calling `compile-js`.
* `name` - The name of the Javascript function.
* `..args` - The arguments to pass to the Javascript function. All other positional arguments are passed to the Javascript function as is.

#### Returns

The result of the Javascript function. The type is the typst type which most closely resembles the Javascript type.

#### Example

```typ
#let bytecode = compile-js("function sum(a, b) { return a + b; }")
#let result = call-js-function(bytecode, "sum", 1, 2)
```

### `list-global-property`

List all global properties of a compiled Javascript bytecode. This is useful for inspecting the compiled Javascript bytecode.

#### Arguments

* `bytecode` - The bytecode of the Javascript function. It can be obtained by calling `compile-js`.

#### Returns

A list of all global properties of the Javascript bytecode. The type is `array`.

#### Example

```typ
#let bytecode = compile-js("function sum(a, b) { return a + b; }")
#let result = list-global-property(bytecode)
```
Loading

0 comments on commit 3951a1d

Please sign in to comment.