mirror of
https://github.com/fluencelabs/asmble
synced 2025-06-14 15:21:23 +00:00
Fix Rust Simple and Rust String examples
This commit is contained in:
@ -31,6 +31,7 @@ fun main(args: Array<String>) {
|
|||||||
val globals = Main.globalArgs(argBuild)
|
val globals = Main.globalArgs(argBuild)
|
||||||
logger = Logger.Print(globals.logLevel)
|
logger = Logger.Print(globals.logLevel)
|
||||||
command.logger = logger
|
command.logger = logger
|
||||||
|
logger.info { "Running the command=${command.name} with args=${argBuild.args}" }
|
||||||
command.runWithArgs(argBuild)
|
command.runWithArgs(argBuild)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
logger.error { "Error ${command?.let { "in command '${it.name}'" } ?: ""}: ${e.message}" }
|
logger.error { "Error ${command?.let { "in command '${it.name}'" } ?: ""}: ${e.message}" }
|
||||||
|
@ -75,7 +75,10 @@ open class Translate : Command<Translate.Args>() {
|
|||||||
fun inToAst(inFile: String, inFormat: String): Script {
|
fun inToAst(inFile: String, inFormat: String): Script {
|
||||||
val inBytes =
|
val inBytes =
|
||||||
if (inFile == "--") System.`in`.use { it.readBytes() }
|
if (inFile == "--") System.`in`.use { it.readBytes() }
|
||||||
else File(inFile).let { f -> FileInputStream(f).use { it.readBytes(f.length().toIntExact()) } }
|
else File(inFile).let { f ->
|
||||||
|
// Input file might not fit into the memory
|
||||||
|
FileInputStream(f).use { it.readBytes(f.length().toIntExact()) }
|
||||||
|
}
|
||||||
return when (inFormat) {
|
return when (inFormat) {
|
||||||
"wast" -> StrToSExpr.parse(inBytes.toString(Charsets.UTF_8)).let { res ->
|
"wast" -> StrToSExpr.parse(inBytes.toString(Charsets.UTF_8)).let { res ->
|
||||||
when (res) {
|
when (res) {
|
||||||
|
@ -150,26 +150,28 @@ open class BinaryToAst(
|
|||||||
|
|
||||||
fun toMemoryType(b: ByteReader) = Node.Type.Memory(toResizableLimits(b))
|
fun toMemoryType(b: ByteReader) = Node.Type.Memory(toResizableLimits(b))
|
||||||
|
|
||||||
fun toModule(b: ByteReader): Node.Module {
|
fun toModule(bytes: ByteReader): Node.Module {
|
||||||
if (b.readUInt32() != 0x6d736100L) throw IoErr.InvalidMagicNumber()
|
if (bytes.readUInt32() != 0x6d736100L) throw IoErr.InvalidMagicNumber()
|
||||||
b.readUInt32().let { if (it != version) throw IoErr.InvalidVersion(it, listOf(version)) }
|
bytes.readUInt32().let { if (it != version) throw IoErr.InvalidVersion(it, listOf(version)) }
|
||||||
|
|
||||||
// Slice up all the sections
|
// Slice up all the sections
|
||||||
var maxSectionId = 0
|
var maxSectionId = 0
|
||||||
var sections = emptyList<Pair<Int, ByteReader>>()
|
var sections = emptyList<Pair<Int, ByteReader>>()
|
||||||
while (!b.isEof) {
|
while (!bytes.isEof) {
|
||||||
val sectionId = b.readVarUInt7().toInt()
|
val sectionId = bytes.readVarUInt7().toInt()
|
||||||
if (sectionId > 11) throw IoErr.InvalidSectionId(sectionId)
|
if (sectionId > 11) throw IoErr.InvalidSectionId(sectionId)
|
||||||
if (sectionId != 0)
|
if (sectionId != 0)
|
||||||
require(sectionId > maxSectionId) { "Section ID $sectionId came after $maxSectionId" }.
|
require(sectionId > maxSectionId) { "Section ID $sectionId came after $maxSectionId" }.
|
||||||
also { maxSectionId = sectionId }
|
also { maxSectionId = sectionId }
|
||||||
val sectionLen = b.readVarUInt32AsInt()
|
val sectionLen = bytes.readVarUInt32AsInt()
|
||||||
sections += sectionId to b.read(sectionLen)
|
// each 'read' invocation creates new InputStream and don't closes it
|
||||||
|
sections += sectionId to bytes.read(sectionLen)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now build the module
|
// Now build the module
|
||||||
fun <T> readSectionList(sectionId: Int, fn: (ByteReader) -> T) =
|
fun <T> readSectionList(sectionId: Int, fn: (ByteReader) -> T) =
|
||||||
sections.find { it.first == sectionId }?.second?.readList(fn) ?: emptyList()
|
sections.find { it.first == sectionId }?.second?.readList(fn) ?: emptyList()
|
||||||
|
|
||||||
val types = readSectionList(1, this::toFuncType)
|
val types = readSectionList(1, this::toFuncType)
|
||||||
val funcIndices = readSectionList(3) { it.readVarUInt32AsInt() }
|
val funcIndices = readSectionList(3) { it.readVarUInt32AsInt() }
|
||||||
return Node.Module(
|
return Node.Module(
|
||||||
|
@ -100,6 +100,7 @@ abstract class ByteReader {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// todo looks like this InputStream isn't ever closed
|
||||||
class InputStream(val ins: java.io.InputStream) : ByteReader() {
|
class InputStream(val ins: java.io.InputStream) : ByteReader() {
|
||||||
private var nextByte: Byte? = null
|
private var nextByte: Byte? = null
|
||||||
private var sawEof = false
|
private var sawEof = false
|
||||||
|
@ -9,9 +9,3 @@ Compile Rust to WASM and then to the JVM. In order of complexity:
|
|||||||
* [rust-simple](rust-simple)
|
* [rust-simple](rust-simple)
|
||||||
* [rust-string](rust-string)
|
* [rust-string](rust-string)
|
||||||
* [rust-regex](rust-regex)
|
* [rust-regex](rust-regex)
|
||||||
|
|
||||||
### C/C++
|
|
||||||
|
|
||||||
Compile C to WASM and then to the JVM. In order of complexity:
|
|
||||||
|
|
||||||
* [c-simple](c-simple)
|
|
@ -1,6 +1,7 @@
|
|||||||
#![feature(allocator_api)]
|
#![feature(allocator_api)]
|
||||||
|
|
||||||
use std::heap::{Alloc, Heap, Layout};
|
use std::ptr::NonNull;
|
||||||
|
use std::alloc::{Alloc, Global, Layout};
|
||||||
use std::ffi::{CString};
|
use std::ffi::{CString};
|
||||||
use std::mem;
|
use std::mem;
|
||||||
use std::os::raw::c_char;
|
use std::os::raw::c_char;
|
||||||
@ -30,17 +31,17 @@ pub extern "C" fn prepend_from_rust(ptr: *mut u8, len: usize) -> *const c_char {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn alloc(size: usize) -> *mut u8 {
|
pub extern "C" fn alloc(size: usize) -> NonNull<u8> {
|
||||||
unsafe {
|
unsafe {
|
||||||
let layout = Layout::from_size_align(size, mem::align_of::<u8>()).unwrap();
|
let layout = Layout::from_size_align(size, mem::align_of::<u8>()).unwrap();
|
||||||
Heap.alloc(layout).unwrap()
|
Global.alloc(layout).unwrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn dealloc(ptr: *mut u8, size: usize) {
|
pub extern "C" fn dealloc(ptr: NonNull<u8>, size: usize) {
|
||||||
unsafe {
|
unsafe {
|
||||||
let layout = Layout::from_size_align(size, mem::align_of::<u8>()).unwrap();
|
let layout = Layout::from_size_align(size, mem::align_of::<u8>()).unwrap();
|
||||||
Heap.dealloc(ptr, layout);
|
Global.dealloc(ptr, layout);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,7 +1,6 @@
|
|||||||
rootProject.name = 'asmble'
|
rootProject.name = 'asmble'
|
||||||
include 'annotations',
|
include 'annotations',
|
||||||
'compiler',
|
'compiler',
|
||||||
'examples:c-simple',
|
|
||||||
'examples:rust-regex',
|
'examples:rust-regex',
|
||||||
'examples:rust-simple',
|
'examples:rust-simple',
|
||||||
'examples:rust-string'
|
'examples:rust-string'
|
Reference in New Issue
Block a user