-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add string ffi UCS2 * chore: add todo
- Loading branch information
Showing
4 changed files
with
160 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,98 @@ | ||
# Test | ||
|
||
Ensure basics of functionality of strings ffi. | ||
|
||
#### main.lys | ||
|
||
```lys | ||
import support::ffi | ||
#[export] fun validateEmptyString(str: UnsafeCPointer): boolean = { | ||
val received = UCS2.fromPtr(str) | ||
received == "" | ||
} | ||
#[export] fun validateNonEmptyString(str: UnsafeCPointer): boolean = { | ||
val received = UCS2.fromPtr(str) | ||
received == "TesT!" | ||
} | ||
#[export] fun getEmptyString(): UnsafeCPointer = { | ||
"" as UnsafeCPointer | ||
} | ||
#[export] fun getNonEmptyString(): UnsafeCPointer = { | ||
"Agus 🚢 💫 ڞ" as UnsafeCPointer | ||
} | ||
#[export] fun identity(str: UnsafeCPointer): UnsafeCPointer = { | ||
UCS2.fromPtr(str) as UnsafeCPointer | ||
} | ||
#[export] fun main(): void = { | ||
// noop | ||
} | ||
``` | ||
|
||
#### assertions.js | ||
|
||
```js | ||
getInstance => { | ||
const ffi = require('./execution') | ||
const instance = getInstance() | ||
const exports = instance.exports | ||
|
||
const errors = [] | ||
|
||
function write(str) { | ||
return ffi.writeStringToHeap(instance, str) | ||
} | ||
|
||
function read(ptr) { | ||
const str = ffi.readStringFromHeap(instance, ptr) | ||
console.log("Read: " + str) | ||
return str | ||
} | ||
|
||
if (!exports.validateEmptyString(write(""))) { | ||
errors.push('validateEmptyString1') | ||
} | ||
|
||
if (!exports.validateNonEmptyString(write("TesT!"))) { | ||
errors.push('validateNonEmptyString1') | ||
} | ||
|
||
if (!exports.validateEmptyString(write(""))) { | ||
errors.push('validateEmptyString2') | ||
} | ||
|
||
if (!exports.validateNonEmptyString(write("TesT!"))) { | ||
errors.push('validateNonEmptyString2') | ||
} | ||
|
||
if (read(exports.getEmptyString()) != "") { | ||
errors.push('getEmptyString') | ||
} | ||
|
||
if (read(exports.getNonEmptyString()) != "Agus 🚢 💫 ڞ") { | ||
errors.push('getNonEmptyString') | ||
} | ||
|
||
['', "a", "1234", "௸", "🥶"].forEach(t => { | ||
if (read(write(t)) != t) { | ||
errors.push(`read(write(${t}))`) | ||
} | ||
}) | ||
|
||
// ['', "a", "1234", "௸", "🥶"].forEach(t => { | ||
// if (read(exports.identity(write(t))) != t) { | ||
// errors.push(`identity ${t}`) | ||
// } | ||
// }) | ||
|
||
if (errors.length) { | ||
throw new Error(errors.join(', ')) | ||
} | ||
}; | ||
``` |
This file contains 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
This file contains 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,27 @@ | ||
private fun stringDiscriminant(): u64 = { | ||
val discriminant: u32 = string.^discriminant | ||
discriminant as u64 << 32 | ||
} | ||
|
||
#[export "ffi_allocateString"] | ||
private fun allocateString(sizeInChars: u32): UnsafeCPointer = { | ||
string.fromBytes(bytes(sizeInChars << 1)) as UnsafeCPointer | ||
} | ||
|
||
type UCS2 = void | ||
|
||
impl UCS2 { | ||
// Converts a pointer resulted from a allocBytes to a string | ||
fun fromPtr(bytesPointer: UnsafeCPointer): string = %wasm { | ||
(i64.or | ||
(call $stringDiscriminant) | ||
(i64.and | ||
(i64.const 0xFFFFFFFF) | ||
(i64.extend_u/i32 | ||
(i32.sub | ||
(get_local $bytesPointer) | ||
(i32.const 4))))) | ||
} | ||
|
||
fun toPtr(str: string): UnsafeCPointer = str as UnsafeCPointer | ||
} |