2018-12-05 23:30:46 +01:00
/ *
2020-01-01 22:58:13 +01:00
Copyright 2018 - 2020 Volker Berlin ( i - net software )
2018-12-05 23:30:46 +01:00
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.module ;
import java.io.IOException ;
2019-04-22 21:24:22 +02:00
import java.util.List ;
2018-12-05 23:30:46 +01:00
import javax.annotation.Nonnull ;
import javax.annotation.Nullable ;
import de.inetsoftware.jwebassembly.WasmException ;
2019-09-08 13:55:22 +02:00
import de.inetsoftware.jwebassembly.javascript.JavaScriptSyntheticFunctionName ;
2019-01-06 16:29:26 +01:00
import de.inetsoftware.jwebassembly.module.TypeManager.StructType ;
2019-01-14 20:09:00 +01:00
import de.inetsoftware.jwebassembly.wasm.AnyType ;
2019-04-21 21:33:22 +02:00
import de.inetsoftware.jwebassembly.wasm.NamedStorageType ;
2018-12-05 23:30:46 +01:00
import de.inetsoftware.jwebassembly.wasm.StructOperator ;
import de.inetsoftware.jwebassembly.wasm.ValueType ;
/ * *
* WasmInstruction for struct operation . A struct is like a Java class without methods .
*
* @author Volker Berlin
*
* /
class WasmStructInstruction extends WasmInstruction {
2019-04-21 21:33:22 +02:00
private final StructOperator op ;
2018-12-05 23:30:46 +01:00
2019-04-21 21:33:22 +02:00
private final StructType type ;
2018-12-05 23:30:46 +01:00
2019-04-21 21:33:22 +02:00
private final NamedStorageType fieldName ;
2019-01-13 11:36:07 +01:00
2019-09-08 13:55:22 +02:00
private SyntheticFunctionName functionName ;
2018-12-05 23:30:46 +01:00
/ * *
* Create an instance of numeric operation .
*
* @param op
* the struct operation
2019-08-14 20:07:39 +02:00
* @param typeName
* the type name of the parameters
2019-01-13 11:36:07 +01:00
* @param fieldName
* the name of field if needed for the operation
2018-12-05 23:30:46 +01:00
* @param javaCodePos
* the code position / offset in the Java method
2019-03-31 11:23:45 +02:00
* @param lineNumber
* the line number in the Java source code
2019-09-08 13:55:22 +02:00
* @param types
* the type manager
2018-12-05 23:30:46 +01:00
* /
2019-08-14 20:07:39 +02:00
WasmStructInstruction ( @Nullable StructOperator op , @Nullable String typeName , @Nullable NamedStorageType fieldName , int javaCodePos , int lineNumber , TypeManager types ) {
2019-03-31 11:23:45 +02:00
super ( javaCodePos , lineNumber ) ;
2018-12-05 23:30:46 +01:00
this . op = op ;
2019-08-14 20:07:39 +02:00
this . type = typeName = = null ? null : types . valueOf ( typeName ) ;
2019-01-13 11:36:07 +01:00
this . fieldName = fieldName ;
2019-08-14 20:07:39 +02:00
if ( type ! = null & & fieldName ! = null ) {
type . useFieldName ( fieldName ) ;
}
2018-12-05 23:30:46 +01:00
}
2019-09-08 13:55:22 +02:00
/ * *
* Create the synthetic polyfill function of this instruction for nonGC mode .
*
* @return the function or null if not needed
* /
SyntheticFunctionName createNonGcFunction ( ) {
switch ( op ) {
case NEW :
case NEW_DEFAULT :
2019-09-14 22:59:12 +02:00
functionName = new JavaScriptSyntheticFunctionName ( " NonGC " , " new_ " + type . getName ( ) . replace ( '/' , '_' ) , ( ) - > {
2019-09-15 15:42:50 +02:00
// create the default values of a new type
StringBuilder js = new StringBuilder ( " () => Object.seal({ " ) ;
List < NamedStorageType > list = type . getFields ( ) ;
for ( int i = 0 ; i < list . size ( ) ; i + + ) {
if ( i > 0 ) {
js . append ( ',' ) ;
}
js . append ( i ) . append ( ':' ) ;
NamedStorageType storageType = list . get ( i ) ;
if ( TypeManager . VTABLE = = storageType . getName ( ) ) {
js . append ( type . getVTable ( ) ) ;
} else {
AnyType fieldType = storageType . getType ( ) ;
if ( fieldType instanceof ValueType & & fieldType ! = ValueType . anyref ) {
js . append ( '0' ) ;
} else {
js . append ( " null " ) ;
}
}
}
js . append ( " }) " ) ;
return js . toString ( ) ;
2019-09-14 22:59:12 +02:00
} , null , type ) ;
2019-09-08 13:55:22 +02:00
break ;
case SET :
2019-09-13 21:05:47 +02:00
AnyType fieldType = fieldName . getType ( ) ;
2020-01-05 21:41:19 +01:00
functionName = new JavaScriptSyntheticFunctionName ( " NonGC " , " set_ " + validJsName ( fieldType ) , ( ) - > " (a,v,i) => a[i]=v " , ValueType . anyref , fieldType , ValueType . i32 , null , null ) ;
2019-09-08 13:55:22 +02:00
break ;
case GET :
2019-09-13 21:05:47 +02:00
fieldType = fieldName . getType ( ) ;
2020-01-05 21:41:19 +01:00
functionName = new JavaScriptSyntheticFunctionName ( " NonGC " , " get_ " + validJsName ( fieldType ) , ( ) - > " (a,i) => a[i] " , ValueType . anyref , ValueType . i32 , null , fieldType ) ;
2019-09-08 13:55:22 +02:00
break ;
default :
}
return functionName ;
}
2020-01-05 21:41:19 +01:00
/ * *
* Get a valid JavaScript name .
* @param type the type
* @return the identifier that is valid
* /
private static String validJsName ( AnyType type ) {
return type instanceof StructType ? " anyref " : type . toString ( ) ;
}
2019-09-08 13:55:22 +02:00
2019-04-20 21:41:46 +02:00
/ * *
* Get the StructOperator
*
* @return the operator
* /
StructOperator getOperator ( ) {
return op ;
}
2018-12-19 20:10:26 +01:00
/ * *
2019-01-23 20:27:57 +01:00
* Get the struct type of this instruction .
2018-12-19 20:10:26 +01:00
*
* @return the type
* /
2019-01-23 20:27:57 +01:00
StructType getStructType ( ) {
return type ;
2018-12-19 20:10:26 +01:00
}
2018-12-16 18:22:44 +01:00
/ * *
* { @inheritDoc }
* /
@Override
Type getType ( ) {
return Type . Struct ;
}
2018-12-05 23:30:46 +01:00
/ * *
* { @inheritDoc }
* /
public void writeTo ( @Nonnull ModuleWriter writer ) throws IOException {
2019-04-22 21:24:22 +02:00
int idx = - 1 ;
if ( type ! = null & & fieldName ! = null ) {
// The fieldName of the struct operation does not contain the class name in which the field was declared. It contains the class name of the variable. This can be the class or a subclass.
List < NamedStorageType > fields = type . getFields ( ) ;
2019-08-14 20:07:39 +02:00
boolean classNameMatched = type . getName ( ) . equals ( fieldName . geClassName ( ) ) ;
2019-04-22 21:24:22 +02:00
for ( int i = fields . size ( ) - 1 ; i > = 0 ; i - - ) {
NamedStorageType field = fields . get ( i ) ;
if ( ! classNameMatched & & field . geClassName ( ) . equals ( fieldName . geClassName ( ) ) ) {
classNameMatched = true ;
}
if ( classNameMatched & & field . getName ( ) . equals ( fieldName . getName ( ) ) ) {
idx = i ;
break ;
}
}
2019-08-14 20:07:39 +02:00
if ( ! classNameMatched ) {
// special case, the type self does not add a needed field, that we search in all fields
for ( int i = fields . size ( ) - 1 ; i > = 0 ; i - - ) {
NamedStorageType field = fields . get ( i ) ;
if ( field . getName ( ) . equals ( fieldName . getName ( ) ) ) {
idx = i ;
break ;
}
}
}
2019-04-22 21:24:22 +02:00
}
2019-09-08 13:55:22 +02:00
if ( functionName ! = null ) { // nonGC
2019-09-14 15:22:25 +02:00
if ( fieldName ! = null ) {
writer . writeConst ( idx , ValueType . i32 ) ;
}
2019-09-08 13:55:22 +02:00
writer . writeFunctionCall ( functionName ) ;
} else {
writer . writeStructOperator ( op , type , fieldName , idx ) ;
}
2018-12-05 23:30:46 +01:00
}
/ * *
* { @inheritDoc }
* /
2019-01-20 19:58:23 +01:00
AnyType getPushValueType ( ) {
2018-12-05 23:30:46 +01:00
switch ( op ) {
2019-01-20 11:31:33 +01:00
case NULL :
2018-12-05 23:30:46 +01:00
return ValueType . anyref ;
2019-01-23 20:27:57 +01:00
case NEW :
case NEW_DEFAULT :
2019-01-20 19:58:23 +01:00
return type ;
2020-01-01 22:58:13 +01:00
case GET :
return fieldName . getType ( ) ;
2018-12-05 23:30:46 +01:00
case SET :
return null ;
2020-02-01 16:49:52 +01:00
case INSTANCEOF :
return ValueType . i32 ; // a boolean value
2018-12-05 23:30:46 +01:00
default :
throw new WasmException ( " Unknown array operation: " + op , - 1 ) ;
}
}
/ * *
* { @inheritDoc }
* /
@Override
int getPopCount ( ) {
switch ( op ) {
case GET :
2020-02-01 16:49:52 +01:00
case INSTANCEOF :
2018-12-05 23:30:46 +01:00
return 1 ;
case SET :
2019-07-23 21:48:31 +02:00
return 2 ;
2020-01-05 21:42:36 +01:00
case NEW :
case NEW_DEFAULT :
2019-01-20 19:58:23 +01:00
case NULL :
2018-12-05 23:30:46 +01:00
return 0 ;
default :
throw new WasmException ( " Unknown array operation: " + op , - 1 ) ;
}
}
}