Skip to content
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

ctxjs:0.1.0 #959

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
21 changes: 21 additions & 0 deletions packages/preview/ctxjs/0.1.0/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 lublak

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.
30 changes: 30 additions & 0 deletions packages/preview/ctxjs/0.1.0/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# CtxJS

A typst plugin to evaluate javascript code.

- multiple javascript contexts
- load javascript modules as source or bytecode
- simple evaluations
- formated evaluations (execute your code with your typst data)
- call functions
- call functions in modules
- create bytecode with an extra tool (ctxjs_module_bytecode_builder)

## Example

```typst
#import "@preview/ctxjs:0.1.0"

#{
_ = ctxjs.create_context("context_name")
let test = ctxjs.eval("context_name", "function test(data) {return data + 2;}")
let returns_4 = ctxjs.call_function("context_name", "test", (2,))
let returns_6 = ctxjs.eval_format("context_name", "test({test})", (test:4))
let code = ```
export function another_test_function() { return {data: 'test'}; }
```;
_ = ctxjs.load_module_js("context_name", "module_name", code.text)
let returns_array_with_another_test = ctxjs.get_module_properties("context_name", "module_name")
let returns_data_with_test_string = ctxjs.call_module_function("context_name", "module_name", "another_test_function", ())
}
```
Binary file added packages/preview/ctxjs/0.1.0/ctxjs.wasm
Binary file not shown.
49 changes: 49 additions & 0 deletions packages/preview/ctxjs/0.1.0/lib.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#let ctxjs = plugin("ctxjs.wasm")

#let string_to_bytes(data) = {
let data = data
if type(data) == str {
data = bytes(data)
} else if type(data) == array {
data = bytes(data)
} else if type(data) == content {
data = bytes(data.text)
}
data
}

#let create_context(ctx_name) = {
ctxjs.create_context(string_to_bytes(ctx_name))
}

#let eval(ctx_name, js) = {
cbor.decode(ctxjs.eval(string_to_bytes(ctx_name), string_to_bytes(js)))
}

#let call_function(ctx_name, fn_name, args) = {
cbor.decode(ctxjs.call_function(string_to_bytes(ctx_name), string_to_bytes(fn_name), cbor.encode(args)))
}

#let define_vars(ctx_name, vars) = {
cbor.decode(ctxjs.define_vars(string_to_bytes(ctx_name), cbor.encode(vars)))
}

#let eval_format(ctx_name, js, args) = {
cbor.decode(ctxjs.eval_format(string_to_bytes(ctx_name), string_to_bytes(js), cbor.encode(args)))
}

#let load_module_bytecode(ctx_name, bytecode) = {
ctxjs.load_module_bytecode(string_to_bytes(ctx_name), bytecode)
}

#let load_module_js(ctx_name, module_name, module) = {
ctxjs.load_module_js(string_to_bytes(ctx_name), string_to_bytes(module_name), string_to_bytes(module))
}

#let call_module_function(ctx_name, module_name, fn_name, args) = {
cbor.decode(ctxjs.call_module_function(string_to_bytes(ctx_name), string_to_bytes(module_name), string_to_bytes(fn_name), cbor.encode(args)))
}

#let get_module_properties(ctx_name, module_name) = {
cbor.decode(ctxjs.get_module_properties(string_to_bytes(ctx_name), string_to_bytes(module_name)))
}
20 changes: 20 additions & 0 deletions packages/preview/ctxjs/0.1.0/typst.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "ctxjs"
version = "0.1.0"
entrypoint = "lib.typ"
authors = ["lublak"]
license = "MIT"
description = "Run javascript in contexts."

homepage = "https://github.com/lublak/typst-ctxjs-package"
repository = "https://github.com/lublak/typst-ctxjs-package"

keywords = [
"js",
"javascript",
"quickjs",
"runtime",
"context",
"modules",
"vm",
]
Loading