mirror of
https://github.com/i-net-software/JWebAssembly.git
synced 2025-03-25 07:27:52 +01:00
prepare the using of memory and data section
This commit is contained in:
parent
fbc687ee92
commit
8e439eb83e
@ -63,6 +63,8 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
|
|||||||
|
|
||||||
private final boolean createSourceMap;
|
private final boolean createSourceMap;
|
||||||
|
|
||||||
|
private WasmOutputStream dataStream = new WasmOutputStream();
|
||||||
|
|
||||||
private WasmOutputStream codeStream = new WasmOutputStream();
|
private WasmOutputStream codeStream = new WasmOutputStream();
|
||||||
|
|
||||||
private List<TypeEntry> functionTypes = new ArrayList<>();
|
private List<TypeEntry> functionTypes = new ArrayList<>();
|
||||||
@ -121,10 +123,12 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
|
|||||||
writeSection( SectionType.Type, functionTypes );
|
writeSection( SectionType.Type, functionTypes );
|
||||||
writeSection( SectionType.Import, imports.values() );
|
writeSection( SectionType.Import, imports.values() );
|
||||||
writeSection( SectionType.Function, functions.values() );
|
writeSection( SectionType.Function, functions.values() );
|
||||||
|
writeMemorySection();
|
||||||
writeSection( SectionType.Global, globals.values() );
|
writeSection( SectionType.Global, globals.values() );
|
||||||
writeEventSection();
|
writeEventSection();
|
||||||
writeExportSection();
|
writeExportSection();
|
||||||
writeCodeSection();
|
writeCodeSection();
|
||||||
|
writeDataSection();
|
||||||
writeDebugNames();
|
writeDebugNames();
|
||||||
writeSourceMappingUrl();
|
writeSourceMappingUrl();
|
||||||
writeProducersSection();
|
writeProducersSection();
|
||||||
@ -154,6 +158,25 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write the memory section.
|
||||||
|
*
|
||||||
|
* @throws IOException
|
||||||
|
* if any I/O error occur
|
||||||
|
*/
|
||||||
|
private void writeMemorySection() throws IOException {
|
||||||
|
WasmOutputStream stream = new WasmOutputStream();
|
||||||
|
int pages = (dataStream.size() + 0xFFFF) / 0x10000; // a page is defined with a size of 64KiB
|
||||||
|
int count = 1;
|
||||||
|
stream.writeVaruint32( count );
|
||||||
|
for( int i = 0; i < count; i++ ) {
|
||||||
|
stream.writeVaruint32( 1 ); // flags; 1-maximum is available, 0-no maximum value available
|
||||||
|
stream.writeVaruint32( pages ); // initial length
|
||||||
|
stream.writeVaruint32( pages ); // maximum length
|
||||||
|
}
|
||||||
|
wasm.writeSection( SectionType.Memory, stream );
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write the event section if needed.
|
* Write the event section if needed.
|
||||||
*
|
*
|
||||||
@ -233,6 +256,31 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write the data section
|
||||||
|
*
|
||||||
|
* @throws IOException
|
||||||
|
* if any I/O error occur
|
||||||
|
*/
|
||||||
|
private void writeDataSection() throws IOException {
|
||||||
|
int size = dataStream.size();
|
||||||
|
if( size == 0 ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
WasmOutputStream stream = new WasmOutputStream();
|
||||||
|
stream.writeVaruint32( 1 ); // count, we use one large segment
|
||||||
|
|
||||||
|
// one data segment
|
||||||
|
stream.writeVaruint32( 0 ); // index (0 in the MVP)
|
||||||
|
stream.writeConst( 0, ValueType.i32 ); // the offset on which the data start
|
||||||
|
stream.writeOpCode( END ); // end of offset instruction
|
||||||
|
stream.writeVaruint32( size );
|
||||||
|
dataStream.writeTo( stream );
|
||||||
|
|
||||||
|
wasm.writeSection( SectionType.Data, stream );
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write optional the debug names into the name section
|
* Write optional the debug names into the name section
|
||||||
*
|
*
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
package de.inetsoftware.jwebassembly.text;
|
package de.inetsoftware.jwebassembly.text;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -52,6 +53,8 @@ public class TextModuleWriter extends ModuleWriter {
|
|||||||
|
|
||||||
private final boolean debugNames;
|
private final boolean debugNames;
|
||||||
|
|
||||||
|
private final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
|
||||||
|
|
||||||
private final ArrayList<String> methodParamNames = new ArrayList<>();
|
private final ArrayList<String> methodParamNames = new ArrayList<>();
|
||||||
|
|
||||||
private StringBuilder methodOutput = new StringBuilder();
|
private StringBuilder methodOutput = new StringBuilder();
|
||||||
@ -90,6 +93,22 @@ public class TextModuleWriter extends ModuleWriter {
|
|||||||
@Override
|
@Override
|
||||||
public void close() throws IOException {
|
public void close() throws IOException {
|
||||||
output.append( methodOutput );
|
output.append( methodOutput );
|
||||||
|
|
||||||
|
int dataSize = dataStream.size();
|
||||||
|
if( dataSize > 0 ) {
|
||||||
|
int pages = (dataSize + 0xFFFF) / 0x10000;
|
||||||
|
newline( output );
|
||||||
|
String pagesStr = Integer.toString( pages );
|
||||||
|
output.append( "(memory " ).append( pagesStr ).append( ' ' ).append( pagesStr ).append( ')' );
|
||||||
|
newline( output );
|
||||||
|
output.append( "(data (i32.const 0) \"" );
|
||||||
|
byte[] data = dataStream.toByteArray();
|
||||||
|
for( byte b : data ) {
|
||||||
|
output.append( '\\' ).append( Character.forDigit( (b >> 4) & 0xF, 16 ) ).append( Character.forDigit( b & 0xF, 16 ) );
|
||||||
|
}
|
||||||
|
output.append( "\")" );
|
||||||
|
}
|
||||||
|
|
||||||
inset--;
|
inset--;
|
||||||
newline( output );
|
newline( output );
|
||||||
output.append( ')' );
|
output.append( ')' );
|
||||||
|
Loading…
x
Reference in New Issue
Block a user