Skip to content

Commit a45d475

Browse files
committed
Convert global browser bindings to functions
1 parent b672d2c commit a45d475

5 files changed

Lines changed: 41 additions & 29 deletions

File tree

docs/wasm-gc-codegen.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ Without disambiguation, duplicate JS object keys cause the last one to silently
317317
|---------------|-----------|--------|-------------|
318318
| `js_value.mbt` | `js_value.mbt` (type, trait, `unsafe_cast`) | `js_value_js.mbt` (`extern "js"` for undefined/null/isNull, `js_of`) | `js_value_wasm.mbt` (wasm imports, `jsvalue_to_string` workaround) |
319319
| `primitives.mbt` || `primitives_js.mbt` (all `%identity`) | `primitives_wasm.mbt` (FFI calls for value types, `%identity` for String) |
320-
| `global.mbt` | `global.mbt` (`pub let document/window/navigator`) | `global_js.mbt` (`extern "js"` FFI) | `global_wasm.mbt` (wasm imports) |
320+
| `global.mbt` | `global.mbt` (`pub fn document()/window()/navigator()`) | `global_js.mbt` (`extern "js"` FFI) | `global_wasm.mbt` (wasm imports) |
321321

322322
Files that needed no splitting (already cross-target compatible):
323323
- `js_array.mbt` — uses `= "JsArray" "empty"` syntax

webapi/README.mbt.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ fn readme_counter() -> Unit {
7878
let mut count = 0
7979
8080
// Create count display element
81-
let count_display : HTMLDivElement = document.create_element("div").into()
81+
let count_display : HTMLDivElement = document().create_element("div").into()
8282
count_display
8383
..set_attribute("id", "count-display")
8484
.set_attribute("style", "font-size: 3em; margin: 0.5em 0;")
@@ -89,15 +89,15 @@ fn readme_counter() -> Unit {
8989
}
9090
9191
// Create increment button — closures are accepted directly
92-
let increment_btn = document.create_element("button")
92+
let increment_btn = document().create_element("button")
9393
increment_btn.set_text_content("+")
9494
increment_btn.add_event_listener("click", fn(_event) {
9595
count = count + 1
9696
update_display()
9797
})
9898
9999
// Append to DOM
100-
let app : Element = document.get_element_by_id("app")
100+
let app : Element = document().get_element_by_id("app")
101101
app.append_child(count_display) |> ignore
102102
app.append_child(increment_btn) |> ignore
103103
}
@@ -129,10 +129,10 @@ Demonstrates the Canvas 2D API with gradients, shapes, and text:
129129
```moonbit nocheck
130130
///|
131131
fn readme_canvas() -> Unit {
132-
let canvas : HTMLCanvasElement = document.create_element("canvas").into()
132+
let canvas : HTMLCanvasElement = document().create_element("canvas").into()
133133
canvas.set_width(800)
134134
canvas.set_height(500)
135-
let app : Element = document.get_element_by_id("app")
135+
let app : Element = document().get_element_by_id("app")
136136
app.append_child(canvas) |> ignore
137137
138138
// Get 2D rendering context
@@ -214,19 +214,19 @@ npx serve .
214214

215215
### Global Objects
216216

217-
The library provides direct access to browser global objects:
217+
The library provides zero-argument accessors for browser global objects:
218218

219219
```moonbit nocheck
220220
///|
221221
fn readme_globals() -> Unit {
222222
// Access the document object
223-
let _ = document.get_element_by_id("my-id")
223+
let _ = document().get_element_by_id("my-id")
224224
225225
// Access the window object
226-
let _ = window.inner_width()
226+
let _ = window().inner_width()
227227
228228
// Access the navigator object
229-
let _ = navigator.user_agent()
229+
let _ = navigator().user_agent()
230230
}
231231
```
232232

@@ -238,7 +238,7 @@ DOM elements are returned as generic `Element` types. Use `into()` to cast to sp
238238
///|
239239
fn readme_casting() -> Unit {
240240
// Create an element and cast to specific type
241-
let canvas : HTMLCanvasElement = document.create_element("canvas").into()
241+
let canvas : HTMLCanvasElement = document().create_element("canvas").into()
242242
243243
// Cast to access type-specific methods
244244
let _ctx : CanvasRenderingContext2D = canvas.get_context("2d").unwrap().into()
@@ -256,18 +256,18 @@ Methods that return a nullable interface type (e.g., `Element?`) have two varian
256256
///|
257257
fn readme_opt_methods() -> Unit {
258258
// Convenience: returns Element directly (panics if not found)
259-
let app = document.get_element_by_id("app")
259+
let app = document().get_element_by_id("app")
260260
261261
// Cast to a specific subtype with .into():
262-
let canvas : HTMLCanvasElement = document
262+
let canvas : HTMLCanvasElement = document()
263263
.get_element_by_id("my-canvas")
264264
.into()
265265
266266
// Works on subtypes too (e.g., ShadowRoot inherits query_selector from trait):
267267
// shadow.query_selector("[data-ref=display]")
268268
269269
// _opt variant: returns Option for null-checking
270-
match document.get_element_by_id_opt("maybe-missing") {
270+
match document().get_element_by_id_opt("maybe-missing") {
271271
Some(el) => el.set_text_content("found")
272272
None => ()
273273
}
@@ -284,7 +284,7 @@ Event listeners and handlers accept closures directly:
284284
```moonbit nocheck
285285
///|
286286
fn readme_events() -> Unit {
287-
let element = document.create_element("button")
287+
let element = document().create_element("button")
288288
289289
// addEventListener with closure
290290
element.add_event_listener("click", fn(_event) { println("Clicked!") })
@@ -298,7 +298,7 @@ Use `JsPromise` to chain async operations like `fetch()`:
298298
```moonbit nocheck
299299
///|
300300
fn readme_promises() -> Unit {
301-
window
301+
window()
302302
.fetch("https://api.example.com/data")
303303
.then(fn(response : Response) {
304304
response.text().then(fn(text : String) { Console::log([text]) }) |> ignore
@@ -313,7 +313,7 @@ On the JS backend, the `bikallem/webapi/js_promise` subpackage bridges `JsPromis
313313
```moonbit nocheck
314314
///|
315315
async fn readme_fetch_async(url : String) -> Unit {
316-
let response : Response = @js_promise.to_async_promise(window.fetch(url)).wait()
316+
let response : Response = @js_promise.to_async_promise(window().fetch(url)).wait()
317317
let text : String = @js_promise.to_async_promise(response.text()).wait()
318318
Console::log([text])
319319
}
@@ -353,7 +353,7 @@ Most setter methods return `Unit`, enabling method chaining with `..` (use `.` f
353353
```moonbit nocheck
354354
///|
355355
fn readme_chaining() -> Unit {
356-
let element = document.create_element("div")
356+
let element = document().create_element("div")
357357
element..set_attribute("id", "my-element").set_attribute("class", "container")
358358
element.set_text_content("Hello!")
359359
}
@@ -366,10 +366,10 @@ Many methods have optional parameters using MoonBit's `?` syntax:
366366
```moonbit nocheck
367367
fn readme_optional() -> Unit {
368368
// With default options
369-
let _ = document.create_element("div")
369+
let _ = document().create_element("div")
370370
371371
// With explicit options
372-
let _ = document.create_element(
372+
let _ = document().create_element(
373373
"div",
374374
options=ElementCreationOptions::new(is="custom-div"),
375375
)

webapi/global.mbt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
// MoonBit bindings for global objects
22

33
///|
4-
pub let document : Document = global_document_ffi()
4+
pub fn document() -> Document {
5+
global_document_ffi()
6+
}
57

68
///|
7-
pub let window : Window = global_window_ffi()
9+
pub fn window() -> Window {
10+
global_window_ffi()
11+
}
812

913
///|
10-
pub let navigator : Navigator = global_navigator_ffi()
14+
pub fn navigator() -> Navigator {
15+
global_navigator_ffi()
16+
}
1117

1218
///|
1319
#cfg(target="js")

webapi/pkg.generated.mbti

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -340,11 +340,11 @@ pub const X_PATH_RESULT_UNORDERED_NODE_SNAPSHOT_TYPE : UInt = 6
340340

341341
pub fn define_custom_element(String, (HTMLElement) -> Unit, on_connected? : (HTMLElement) -> Unit, on_disconnected? : (HTMLElement) -> Unit, on_adopted? : (HTMLElement) -> Unit, on_attribute_changed? : (HTMLElement, String, JsValue, JsValue) -> Unit, observed_attributes? : Array[String]) -> Unit
342342

343-
pub let document : Document
343+
pub fn document() -> Document
344344

345-
pub let navigator : Navigator
345+
pub fn navigator() -> Navigator
346346

347-
pub let window : Window
347+
pub fn window() -> Window
348348

349349
// Errors
350350

webapi_gen/base.mbt/global.mbt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
// MoonBit bindings for global objects
22

33
///|
4-
pub let document : Document = global_document_ffi()
4+
pub fn document() -> Document {
5+
global_document_ffi()
6+
}
57

68
///|
7-
pub let window : Window = global_window_ffi()
9+
pub fn window() -> Window {
10+
global_window_ffi()
11+
}
812

913
///|
10-
pub let navigator : Navigator = global_navigator_ffi()
14+
pub fn navigator() -> Navigator {
15+
global_navigator_ffi()
16+
}

0 commit comments

Comments
 (0)