mirror of
https://github.com/fluencelabs/asmble
synced 2025-06-12 22:31:22 +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)
|
||||
logger = Logger.Print(globals.logLevel)
|
||||
command.logger = logger
|
||||
logger.info { "Running the command=${command.name} with args=${argBuild.args}" }
|
||||
command.runWithArgs(argBuild)
|
||||
} catch (e: Exception) {
|
||||
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 {
|
||||
val inBytes =
|
||||
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) {
|
||||
"wast" -> StrToSExpr.parse(inBytes.toString(Charsets.UTF_8)).let { res ->
|
||||
when (res) {
|
||||
|
@ -150,26 +150,28 @@ open class BinaryToAst(
|
||||
|
||||
fun toMemoryType(b: ByteReader) = Node.Type.Memory(toResizableLimits(b))
|
||||
|
||||
fun toModule(b: ByteReader): Node.Module {
|
||||
if (b.readUInt32() != 0x6d736100L) throw IoErr.InvalidMagicNumber()
|
||||
b.readUInt32().let { if (it != version) throw IoErr.InvalidVersion(it, listOf(version)) }
|
||||
fun toModule(bytes: ByteReader): Node.Module {
|
||||
if (bytes.readUInt32() != 0x6d736100L) throw IoErr.InvalidMagicNumber()
|
||||
bytes.readUInt32().let { if (it != version) throw IoErr.InvalidVersion(it, listOf(version)) }
|
||||
|
||||
// Slice up all the sections
|
||||
var maxSectionId = 0
|
||||
var sections = emptyList<Pair<Int, ByteReader>>()
|
||||
while (!b.isEof) {
|
||||
val sectionId = b.readVarUInt7().toInt()
|
||||
while (!bytes.isEof) {
|
||||
val sectionId = bytes.readVarUInt7().toInt()
|
||||
if (sectionId > 11) throw IoErr.InvalidSectionId(sectionId)
|
||||
if (sectionId != 0)
|
||||
require(sectionId > maxSectionId) { "Section ID $sectionId came after $maxSectionId" }.
|
||||
also { maxSectionId = sectionId }
|
||||
val sectionLen = b.readVarUInt32AsInt()
|
||||
sections += sectionId to b.read(sectionLen)
|
||||
val sectionLen = bytes.readVarUInt32AsInt()
|
||||
// each 'read' invocation creates new InputStream and don't closes it
|
||||
sections += sectionId to bytes.read(sectionLen)
|
||||
}
|
||||
|
||||
// Now build the module
|
||||
fun <T> readSectionList(sectionId: Int, fn: (ByteReader) -> T) =
|
||||
sections.find { it.first == sectionId }?.second?.readList(fn) ?: emptyList()
|
||||
|
||||
val types = readSectionList(1, this::toFuncType)
|
||||
val funcIndices = readSectionList(3) { it.readVarUInt32AsInt() }
|
||||
return Node.Module(
|
||||
|
@ -100,6 +100,7 @@ abstract class ByteReader {
|
||||
return result
|
||||
}
|
||||
|
||||
// todo looks like this InputStream isn't ever closed
|
||||
class InputStream(val ins: java.io.InputStream) : ByteReader() {
|
||||
private var nextByte: Byte? = null
|
||||
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-string](rust-string)
|
||||
* [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)]
|
||||
|
||||
use std::heap::{Alloc, Heap, Layout};
|
||||
use std::ptr::NonNull;
|
||||
use std::alloc::{Alloc, Global, Layout};
|
||||
use std::ffi::{CString};
|
||||
use std::mem;
|
||||
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]
|
||||
pub extern "C" fn alloc(size: usize) -> *mut u8 {
|
||||
pub extern "C" fn alloc(size: usize) -> NonNull<u8> {
|
||||
unsafe {
|
||||
let layout = Layout::from_size_align(size, mem::align_of::<u8>()).unwrap();
|
||||
Heap.alloc(layout).unwrap()
|
||||
Global.alloc(layout).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn dealloc(ptr: *mut u8, size: usize) {
|
||||
pub extern "C" fn dealloc(ptr: NonNull<u8>, size: usize) {
|
||||
unsafe {
|
||||
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'
|
||||
include 'annotations',
|
||||
'compiler',
|
||||
'examples:c-simple',
|
||||
'examples:rust-regex',
|
||||
'examples:rust-simple',
|
||||
'examples:rust-string'
|
Reference in New Issue
Block a user