-
Notifications
You must be signed in to change notification settings - Fork 6
Add DOMRefTypes
example
#41
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
Merged
Merged
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
053d9d1
Add `DOMRefTypes` example
MaxDesiatov 21a2109
Fix formatting in `Package.swift`
MaxDesiatov 5c13598
Fix title in `index.html`
MaxDesiatov cba2e72
Fix formatting in `config.json`
MaxDesiatov 099f03e
Add license header to `refs.h`
MaxDesiatov 7c32379
Add license header to `dom.h`
MaxDesiatov e307be7
Add license header to `bridge.c`
MaxDesiatov 8454242
Add license header to `SwiftBindings.swift`
MaxDesiatov a96734f
Add license header to `Entrypoint.swift`
MaxDesiatov 7ef78e4
Describe `DOMRefTypes` in `README.md`
MaxDesiatov 07231f6
Use 6.2 in `DOMRefTypes/Package.swift`
MaxDesiatov 12cc21d
Update pull_request.yml
MaxDesiatov d917226
Merge branch 'main' into maxd/externref
MaxDesiatov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[*] | ||
indent_style = space | ||
indent_size = 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"swiftPM": { | ||
"swiftSDK": "swift-DEVELOPMENT-SNAPSHOT-2025-08-02-a_wasm-embedded" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// swift-tools-version: 6.0 | ||
// The swift-tools-version declares the minimum version of Swift required to build this package. | ||
|
||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift open source project | ||
// | ||
// Copyright (c) 2024 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See http://swift.org/LICENSE.txt for license information | ||
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import PackageDescription | ||
|
||
let linkerSettings: [LinkerSetting] = [ | ||
.unsafeFlags([ | ||
"-Xclang-linker", "-mexec-model=reactor", | ||
"-Xlinker", "--export-if-defined=__main_argc_argv", | ||
]), | ||
] | ||
|
||
let package = Package( | ||
name: "Guest", | ||
targets: [ | ||
.target( | ||
name: "externref", | ||
), | ||
.executableTarget( | ||
name: "RefsTest", | ||
dependencies: ["externref"], | ||
linkerSettings: linkerSettings, | ||
), | ||
] | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift open source project | ||
// | ||
// Copyright (c) 2024-2025 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
@main | ||
struct Entrypoint { | ||
static func main() { | ||
var h1 = Document.global.createElement(name: JSString("h1")) | ||
let body = Document.global.body | ||
body.append(child: h1) | ||
h1.innerHTML = JSString("Hello, world!") | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift open source project | ||
// | ||
// Copyright (c) 2024-2025 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import externref | ||
|
||
struct JSObject: ~Copyable { | ||
fileprivate let ref: ExternRefIndex | ||
|
||
deinit { | ||
freeExternRef(ref) | ||
} | ||
} | ||
|
||
struct JSArray: ~Copyable { | ||
private let ref: ExternRefIndex | ||
|
||
init() { | ||
self.ref = emptyArray() | ||
} | ||
|
||
func append(_ object: borrowing JSObject) { | ||
arrayPush(ref, object.ref) | ||
} | ||
} | ||
|
||
struct JSString: ~Copyable { | ||
fileprivate let ref: ExternRefIndex | ||
|
||
deinit { | ||
freeExternRef(self.ref) | ||
} | ||
} | ||
|
||
extension JSString { | ||
init(_ string: StaticString) { | ||
self.ref = bridgeString(string.utf8Start, string.utf8CodeUnitCount) | ||
} | ||
|
||
} | ||
|
||
struct HTMLElement: ~Copyable { | ||
fileprivate let ref: ExternRefIndex | ||
|
||
func append(child: borrowing HTMLElement) { | ||
appendChild(self.ref, child.ref) | ||
} | ||
|
||
static let innerHTMLName = JSString("innerHTML") | ||
|
||
var innerHTML: JSString { | ||
|
||
get { | ||
JSString(ref: getProp(self.ref, Self.innerHTMLName.ref)) | ||
} | ||
|
||
set { | ||
setProp(self.ref, Self.innerHTMLName.ref, newValue.ref) | ||
} | ||
} | ||
} | ||
|
||
struct Document: ~Copyable { | ||
fileprivate let object: JSObject | ||
|
||
static let global = Document(object: JSObject(ref: getDocument())) | ||
|
||
static let bodyName = JSString("body") | ||
|
||
func createElement(name: borrowing JSString) -> HTMLElement { | ||
.init(ref: externref.createElement(name.ref)) | ||
} | ||
|
||
var body: HTMLElement { | ||
HTMLElement(ref: getProp(self.object.ref, Self.bodyName.ref)) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift open source project | ||
// | ||
// Copyright (c) 2024-2025 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "dom.h" | ||
#include "refs.h" | ||
#include <stdint.h> | ||
|
||
static __externref_t table[0]; | ||
|
||
typedef __externref_t (*__funcref funcref_t)(__externref_t); | ||
static funcref_t ftable[0]; | ||
|
||
static int nextAvailableTableIndex = 0; | ||
static const int defaultTableGrowSize = 256; | ||
|
||
void freeExternRef(ExternRefIndex ref) { | ||
__builtin_wasm_table_set(table, ref.index, __builtin_wasm_ref_null_extern()); | ||
} | ||
|
||
ExternRefIndex tableAppend(__externref_t ref) { | ||
ExternRefIndex idx = { .index = nextAvailableTableIndex++ }; | ||
|
||
if (idx.index >= __builtin_wasm_table_size(table)) { | ||
__builtin_wasm_table_grow(table, __builtin_wasm_ref_null_extern(), defaultTableGrowSize); | ||
} | ||
|
||
__builtin_wasm_table_set(table, idx.index, ref); | ||
|
||
return idx; | ||
} | ||
|
||
ExternRefIndex createElement(ExternRefIndex name) { | ||
return tableAppend(createElementJS(__builtin_wasm_table_get(table, name.index))); | ||
} | ||
|
||
ExternRefIndex getDocument() { | ||
return tableAppend(getDocumentJS()); | ||
} | ||
|
||
ExternRefIndex getProp(ExternRefIndex self, ExternRefIndex name) { | ||
return tableAppend(getPropJS(__builtin_wasm_table_get(table, self.index), __builtin_wasm_table_get(table, name.index))); | ||
} | ||
|
||
int getIntProp(ExternRefIndex self, ExternRefIndex name) { | ||
return getIntPropJS(__builtin_wasm_table_get(table, self.index), __builtin_wasm_table_get(table, name.index)); | ||
} | ||
|
||
void setProp(ExternRefIndex self, ExternRefIndex name, ExternRefIndex val) { | ||
setPropJS(__builtin_wasm_table_get(table, self.index), __builtin_wasm_table_get(table, name.index), __builtin_wasm_table_get(table, val.index)); | ||
} | ||
|
||
void appendChild(ExternRefIndex self, ExternRefIndex child) { | ||
appendChildJS(__builtin_wasm_table_get(table, self.index), __builtin_wasm_table_get(table, child.index)); | ||
} | ||
|
||
ExternRefIndex bridgeString(const uint8_t *str, size_t bytes) { | ||
return tableAppend(bridgeStringJS(str, bytes)); | ||
} | ||
|
||
ExternRefIndex emptyArray() { | ||
return tableAppend(emptyArrayJS()); | ||
} | ||
|
||
void arrayPush(ExternRefIndex self, ExternRefIndex element) { | ||
arrayPushJS(__builtin_wasm_table_get(table, self.index), __builtin_wasm_table_get(table, element.index)); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift open source project | ||
// | ||
// Copyright (c) 2024-2025 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#pragma once | ||
|
||
#include <stdint.h> | ||
|
||
__attribute__((import_module("js"))) | ||
__attribute__((import_name("getDocument"))) __externref_t | ||
getDocumentJS(void); | ||
|
||
__attribute__((import_module("js"))) | ||
__attribute__((import_name("emptyDictionary"))) __externref_t | ||
emptyDictionaryJS(void); | ||
|
||
__attribute__((import_module("js"))) | ||
__attribute__((import_name("emptyArray"))) __externref_t | ||
emptyArrayJS(void); | ||
|
||
__attribute__((import_module("js"))) | ||
__attribute__((import_name("arrayPush"))) void | ||
arrayPushJS(__externref_t self, __externref_t element); | ||
|
||
__attribute__((import_module("js"))) | ||
__attribute__((import_name("bridgeString"))) __externref_t | ||
bridgeStringJS(const uint8_t *str, uint32_t bytes); | ||
|
||
__attribute__((import_module("js"))) | ||
__attribute__((import_name("setProp"))) void | ||
setPropJS(__externref_t self, __externref_t name, __externref_t val); | ||
|
||
__attribute__((import_module("js"))) | ||
__attribute__((import_name("getProp"))) __externref_t | ||
getPropJS(__externref_t self, __externref_t name); | ||
|
||
|
||
__attribute__((import_module("js"))) | ||
__attribute__((import_name("getIntProp"))) int | ||
getIntPropJS(__externref_t self, __externref_t name); | ||
|
||
__attribute__((import_module("document"))) | ||
__attribute__((import_name("createElement"))) __externref_t | ||
createElementJS(__externref_t name); | ||
|
||
__attribute__((import_module("document"))) | ||
__attribute__((import_name("appendChild"))) void | ||
appendChildJS(__externref_t self, __externref_t child); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift open source project | ||
// | ||
// Copyright (c) 2024-2025 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#pragma once | ||
|
||
#include <stddef.h> | ||
#include <stdint.h> | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif /* __cplusplus */ | ||
|
||
typedef struct ExternRefIndex { | ||
int index; | ||
} ExternRefIndex; | ||
|
||
void freeExternRef(ExternRefIndex); | ||
|
||
ExternRefIndex createElement(ExternRefIndex name); | ||
ExternRefIndex getDocument(void); | ||
ExternRefIndex getProp(ExternRefIndex self, ExternRefIndex name); | ||
int getIntProp(ExternRefIndex self, ExternRefIndex name); | ||
void setProp(ExternRefIndex self, ExternRefIndex name, ExternRefIndex val); | ||
void appendChild(ExternRefIndex self, ExternRefIndex child); | ||
ExternRefIndex bridgeString(const uint8_t *str, size_t bytes); | ||
ExternRefIndex emptyArray(void); | ||
void arrayPush(ExternRefIndex self, ExternRefIndex element); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif /* __cplusplus */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<!-- This source file is part of the Swift open source project | ||
|
||
Copyright (c) 2024 Apple Inc. and the Swift project authors | ||
Licensed under Apache License v2.0 with Runtime Library Exception | ||
|
||
See https://swift.org/LICENSE.txt for license information | ||
See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors --> | ||
|
||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Swift for WebAssembly Examples</title> | ||
<script type="module" src="index.js"> | ||
</script> | ||
<style> | ||
body { | ||
background-color: black; | ||
padding: 1rem; | ||
} | ||
h1 { | ||
font-family: sans-serif; | ||
color: white; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<h1 id="wasm-logger">Wasm Reference Types Demo</h1> | ||
</body> | ||
</html> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.