Skip to content

Commit

Permalink
next step for source map #6
Browse files Browse the repository at this point in the history
  • Loading branch information
Horcrux7 committed Mar 31, 2019
1 parent e5036cc commit 0b1ff00
Show file tree
Hide file tree
Showing 7 changed files with 465 additions and 14 deletions.
13 changes: 9 additions & 4 deletions src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import de.inetsoftware.jwebassembly.module.ModuleWriter;
import de.inetsoftware.jwebassembly.module.ValueTypeConvertion;
import de.inetsoftware.jwebassembly.module.WasmTarget;
import de.inetsoftware.jwebassembly.sourcemap.SourceMapWriter;
import de.inetsoftware.jwebassembly.wasm.AnyType;
import de.inetsoftware.jwebassembly.wasm.ArrayOperator;
import de.inetsoftware.jwebassembly.wasm.NamedStorageType;
Expand Down Expand Up @@ -82,6 +83,8 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod

private int exceptionSignatureIndex = -1;

private String javaSourceFile;

/**
* Create new instance.
*
Expand Down Expand Up @@ -202,12 +205,16 @@ private void writeCodeSection() throws IOException {
if( size == 0 ) {
return;
}
SourceMapWriter sourceMap = new SourceMapWriter();
WasmOutputStream stream = new WasmOutputStream();
stream.writeVaruint32( size );
for( Function func : functions.values() ) {
func.functionsStream.writeTo( stream );
}
wasm.writeSection( SectionType.Code, stream );
if( createSourceMap ) {
sourceMap.generate( target.getSourceMapOutput() );
}
}

/**
Expand Down Expand Up @@ -358,9 +365,7 @@ protected void writeExport( FunctionName name, String exportName ) throws IOExce
@Override
protected void writeMethodStart( FunctionName name, String sourceFile ) throws IOException {
function = getFunction( name );
if( createSourceMap ) {
function.sourceFile = sourceFile;
}
this.javaSourceFile = sourceFile;
functionType = new FunctionTypeEntry();
codeStream.reset();
locals.clear();
Expand Down Expand Up @@ -409,7 +414,7 @@ protected void writeMethodParamFinish() throws IOException {
@Override
protected void markCodePosition( int javaCodePosition ) {
if( createSourceMap ) {
function.markCodePosition( codeStream.size(), javaCodePosition );
function.markCodePosition( codeStream.size(), javaCodePosition, javaSourceFile );
}
}

Expand Down
24 changes: 15 additions & 9 deletions src/de/inetsoftware/jwebassembly/binary/Function.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,27 @@
package de.inetsoftware.jwebassembly.binary;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import de.inetsoftware.jwebassembly.sourcemap.SourceMapping;

/**
* An entry in the function section of the WebAssembly.
*
* @author Volker Berlin
*/
class Function extends SectionEntry {

int id;
int id;

int typeId;
int typeId;

List<String> paramNames;
List<String> paramNames;

WasmOutputStream functionsStream;
WasmOutputStream functionsStream;

String sourceFile;
ArrayList<SourceMapping> sourceMappings;

/**
* {@inheritDoc}
Expand All @@ -48,12 +51,15 @@ void writeSectionEntry( WasmOutputStream stream ) throws IOException {
*
* @param streamPosition
* the position in the function stream
* @param javaCodePosition
* @param javaSourceLine
* the position in the Java Source file
* @param sourceFileName
* the name of the Java source file
*/
void markCodePosition( int streamPosition, int javaCodePosition ) {
if( sourceFile != null ) {
// TODO Auto-generated method stub
void markCodePosition( int streamPosition, int javaSourceLine, String sourceFileName ) {
if( sourceMappings == null ) {
sourceMappings = new ArrayList<>();
}
sourceMappings.add( new SourceMapping( streamPosition, javaSourceLine, sourceFileName ) );
}
}
27 changes: 26 additions & 1 deletion src/de/inetsoftware/jwebassembly/module/WasmTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;

import javax.annotation.Nonnull;

Expand All @@ -33,6 +36,8 @@ public class WasmTarget implements Closeable {

private OutputStream output;

private Writer sourceMap;

/**
* Create a target with a file.
*
Expand Down Expand Up @@ -68,6 +73,21 @@ public OutputStream getWasmOutput() throws IOException {
return output;
}

/**
* Get the source map OutputStream
*
* @return the stream
* @throws IOException
* if any I/O error occur
*/
@Nonnull
public Writer getSourceMapOutput() throws IOException {
if( sourceMap == null && file != null ) {
sourceMap = new OutputStreamWriter( new BufferedOutputStream( new FileOutputStream( file + ".map" ) ), StandardCharsets.UTF_8 );
}
return sourceMap;
}

/**
* Close all streams
*
Expand All @@ -76,6 +96,11 @@ public OutputStream getWasmOutput() throws IOException {
*/
@Override
public void close() throws IOException {
output.close();
if( output != null ) {
output.close();
}
if( sourceMap != null ) {
sourceMap.close();
}
}
}
75 changes: 75 additions & 0 deletions src/de/inetsoftware/jwebassembly/sourcemap/Base64VLQ.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2019 Volker Berlin (i-net software)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.inetsoftware.jwebassembly.sourcemap;

import java.io.IOException;

/**
* Encode an integer value as Base64VLQ
*/
class Base64VLQ {
private static final int VLQ_BASE_SHIFT = 5;

private static final int VLQ_BASE = 1 << VLQ_BASE_SHIFT;

private static final int VLQ_BASE_MASK = VLQ_BASE - 1;

private static final int VLQ_CONTINUATION_BIT = VLQ_BASE;

private static final String BASE64_MAP = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

/**
* no instance
*/
private Base64VLQ() {
// nothing
}

/**
* Move the signet bit from the first position (two-complement value) to the last bit position.
*
* examples: 1 -> 2; -1 -> 3; 2 -> 4; -2 -> 5
*
* @param value
* two-complement value
* @return converted value
*/
private static int toVLQSigned( int value ) {
return (value < 0) ? (((-value) << 1) + 1) : ((value << 1) + 0);
}

/**
* Writes a VLQ encoded value to the provide target.
*
* @param out
* the target
* @param value
* the value
* @throws IOException
* if any I/O error occur
*/
static void appendBase64VLQ( Appendable out, int value ) throws IOException {
value = toVLQSigned( value );
do {
int digit = value & VLQ_BASE_MASK;
value >>>= VLQ_BASE_SHIFT;
if( value > 0 ) {
digit |= VLQ_CONTINUATION_BIT;
}
out.append( BASE64_MAP.charAt( digit ) );
} while( value > 0 );
}
}
Loading

0 comments on commit 0b1ff00

Please sign in to comment.