private boolean matchSignature(ClassHierarchy hierarchy, MethodReader functionMethod, MethodReader candidateMethod) { if (functionMethod.parameterCount() > candidateMethod.parameterCount()) { return false; } for (int i = 0; i < functionMethod.parameterCount(); ++i) { if (!hierarchy.isSuperType(functionMethod.parameterType(i), candidateMethod.parameterType(i), false)) { return false; } } return true; }
private boolean isProperSetter(MethodReader method) { if (method.parameterCount() != 1 || !typeHelper.isSupportedType(method.parameterType(0)) || method.getResultType() != ValueType.VOID) { return false; } return extractSuggestedPropertyName(method) != null || isProperPrefix(method.getName(), "set"); }
private boolean processIndexer(MethodReader method, CallLocation callLocation, InvokeInstruction invoke) { if (isProperGetIndexer(method.getDescriptor())) { Variable result = invoke.getReceiver() != null ? program.createVariable() : null; addIndexerGet(invoke.getInstance(), marshaller.wrapArgument(callLocation, invoke.getArguments().get(0), method.parameterType(0), false), result, invoke.getLocation()); if (result != null) { result = marshaller.unwrapReturnValue(callLocation, result, method.getResultType()); copyVar(result, invoke.getReceiver(), invoke.getLocation()); } return true; } if (isProperSetIndexer(method.getDescriptor())) { Variable index = marshaller.wrapArgument(callLocation, invoke.getArguments().get(0), method.parameterType(0), false); Variable value = marshaller.wrapArgument(callLocation, invoke.getArguments().get(1), method.parameterType(1), false); addIndexerSet(invoke.getInstance(), index, value, invoke.getLocation()); return true; } diagnostics.error(callLocation, "Method {{m0}} is not a proper native JavaScript indexer " + "declaration", invoke.getMethod()); return false; }
private void validateSignature(MethodReader method) { if (!method.hasModifier(ElementModifier.STATIC)) { if (!typeHelper.isJavaScriptClass(method.getOwnerName())) { diagnostics.error(location, "Can't call method {{m0}} of non-JS class", method.getReference()); } } for (int i = 0; i < method.parameterCount(); ++i) { if (!typeHelper.isSupportedType(method.parameterType(i))) { diagnostics.error(location, "Invalid type {{t0}} of parameter " + (i + 1) + " of method {{m1}}", method.parameterType(i), method.getReference()); } } if (method.getResultType() != ValueType.VOID && !typeHelper.isSupportedType(method.getResultType())) { diagnostics.error(location, "Invalid type {{t0}} of return value of method {{m1}}", method.getResultType(), method.getReference()); } }
writer.print(tmp + " = "); ValueType type = method.hasModifier(ElementModifier.STATIC) ? method.parameterType(i) : i == 0 ? ValueType.object(method.getOwnerName()) : method.parameterType(i - 1); if (type.isObject(String.class)) { writer.print("teavm_stringToC(");
for (int i = 0; i < invoke.getArguments().size(); ++i) { Variable arg = marshaller.wrapArgument(callLocation, invoke.getArguments().get(i), method.parameterType(i), byRefParams[i]); newArguments.add(arg);
if (meta.parameterCount() == 0 || meta.parameterType(0).isObject(Value.class)) { return null; ValueType proxyParam = meta.parameterType(i + paramOffset); if (proxyParam.isObject(ReflectClass.class)) { if (classParamIndex == -1) {
method.parameterType(i), byRefParams[i]); newArgs.add(arg);
private boolean processProperty(MethodReader method, CallLocation callLocation, InvokeInstruction invoke) { if (isProperGetter(method)) { String propertyName = extractSuggestedPropertyName(method); if (propertyName == null) { propertyName = method.getName().charAt(0) == 'i' ? cutPrefix(method.getName(), 2) : cutPrefix(method.getName(), 3); } Variable result = invoke.getReceiver() != null ? program.createVariable() : null; addPropertyGet(propertyName, invoke.getInstance(), result, invoke.getLocation()); if (result != null) { result = marshaller.unwrapReturnValue(callLocation, result, method.getResultType()); copyVar(result, invoke.getReceiver(), invoke.getLocation()); } return true; } if (isProperSetter(method)) { String propertyName = extractSuggestedPropertyName(method); if (propertyName == null) { propertyName = cutPrefix(method.getName(), 3); } Variable wrapped = marshaller.wrapArgument(callLocation, invoke.getArguments().get(0), method.parameterType(0), false); addPropertySet(propertyName, invoke.getInstance(), wrapped, invoke.getLocation()); return true; } diagnostics.error(callLocation, "Method {{m0}} is not a proper native JavaScript property " + "declaration", invoke.getMethod()); return false; }
for (int i = 0; i < callee.parameterCount(); ++i) { args[i] = marshaller.unwrapReturnValue(location, program.variableAt(paramIndex++), callee.parameterType(i));
private void renderCallable(SourceWriter writer, MethodReader method) throws IOException { writer.append("function(obj,").ws().append("args)").ws().append("{").indent().softNewLine(); initClass(writer, method); if (method.getResultType() != ValueType.VOID) { writer.append("return "); } writer.appendMethodBody(method.getReference()); writer.append('('); boolean first = true; if (!method.hasModifier(ElementModifier.STATIC)) { writer.append("obj").ws(); first = false; } for (int i = 0; i < method.parameterCount(); ++i) { if (!first) { writer.append(',').ws(); } first = false; int index = i; unboxIfNecessary(writer, method.parameterType(i), () -> writer.append("args[" + index + "]")); } writer.append(");").softNewLine(); if (method.getResultType() == ValueType.VOID) { writer.append("return null;").softNewLine(); } writer.outdent().append("}"); }
private Variable[] emitArguments(Variable argumentsVar, ReflectMethodImpl reflectMethod) { UnwrapArrayInstruction unwrapInsn = new UnwrapArrayInstruction(ArrayElementType.OBJECT); unwrapInsn.setArray(argumentsVar); unwrapInsn.setReceiver(program.createVariable()); add(unwrapInsn); argumentsVar = unwrapInsn.getReceiver(); Variable[] arguments = new Variable[reflectMethod.getParameterCount()]; for (int i = 0; i < reflectMethod.getParameterCount(); ++i) { IntegerConstantInstruction indexInsn = new IntegerConstantInstruction(); indexInsn.setConstant(i); indexInsn.setReceiver(program.createVariable()); add(indexInsn); GetElementInstruction extractArgInsn = new GetElementInstruction(ArrayElementType.OBJECT); extractArgInsn.setArray(argumentsVar); extractArgInsn.setIndex(indexInsn.getReceiver()); extractArgInsn.setReceiver(program.createVariable()); add(extractArgInsn); arguments[i] = unbox(extractArgInsn.getReceiver(), reflectMethod.method.parameterType(i)); } return arguments; }
writer.append(',').ws(); writer.append(context.typeToClassString(method.parameterType(i)));
for (int i = 0; i < arguments.length; ++i) { arguments[i] = new ValueImpl<>(program.createVariable(), nestedVarContext, methodImpl.method.parameterType(i)); ValueType argType = methodImpl.method.parameterType(i); Variable var = generator.box(arguments[i].innerValue, argType); arguments[i] = new ValueImpl<>(var, nestedVarContext, argType);
private boolean isProperSetter(MethodReader method) { if (method.parameterCount() != 1 || !typeHelper.isSupportedType(method.parameterType(0)) || method.getResultType() != ValueType.VOID) { return false; } return extractSuggestedPropertyName(method) != null || isProperPrefix(method.getName(), "set"); }
private boolean processIndexer(MethodReader method, CallLocation callLocation, InvokeInstruction invoke) { if (isProperGetIndexer(method.getDescriptor())) { Variable result = invoke.getReceiver() != null ? program.createVariable() : null; addIndexerGet(invoke.getInstance(), marshaller.wrapArgument(callLocation, invoke.getArguments().get(0), method.parameterType(0), false), result, invoke.getLocation()); if (result != null) { result = marshaller.unwrapReturnValue(callLocation, result, method.getResultType()); copyVar(result, invoke.getReceiver(), invoke.getLocation()); } return true; } if (isProperSetIndexer(method.getDescriptor())) { Variable index = marshaller.wrapArgument(callLocation, invoke.getArguments().get(0), method.parameterType(0), false); Variable value = marshaller.wrapArgument(callLocation, invoke.getArguments().get(1), method.parameterType(1), false); addIndexerSet(invoke.getInstance(), index, value, invoke.getLocation()); return true; } diagnostics.error(callLocation, "Method {{m0}} is not a proper native JavaScript indexer " + "declaration", invoke.getMethod()); return false; }
private void validateSignature(MethodReader method) { if (!method.hasModifier(ElementModifier.STATIC)) { if (!typeHelper.isJavaScriptClass(method.getOwnerName())) { diagnostics.error(location, "Can't call method {{m0}} of non-JS class", method.getReference()); } } for (int i = 0; i < method.parameterCount(); ++i) { if (!typeHelper.isSupportedType(method.parameterType(i))) { diagnostics.error(location, "Invalid type {{t0}} of parameter " + (i + 1) + " of method {{m1}}", method.parameterType(i), method.getReference()); } } if (method.getResultType() != ValueType.VOID && !typeHelper.isSupportedType(method.getResultType())) { diagnostics.error(location, "Invalid type {{t0}} of return value of method {{m1}}", method.getResultType(), method.getReference()); } }
if (meta.parameterCount() == 0 || meta.parameterType(0).isObject(Value.class)) { return null; ValueType proxyParam = meta.parameterType(i + paramOffset); if (proxyParam.isObject(ReflectClass.class)) { if (classParamIndex == -1) {
private void emitArguments(Variable argumentsVar, ReflectMethodImpl reflectMethod, List<Variable> arguments) { UnwrapArrayInstruction unwrapInsn = new UnwrapArrayInstruction(ArrayElementType.OBJECT); unwrapInsn.setArray(argumentsVar); unwrapInsn.setReceiver(program.createVariable()); add(unwrapInsn); argumentsVar = unwrapInsn.getReceiver(); for (int i = 0; i < reflectMethod.getParameterCount(); ++i) { IntegerConstantInstruction indexInsn = new IntegerConstantInstruction(); indexInsn.setConstant(i); indexInsn.setReceiver(program.createVariable()); add(indexInsn); GetElementInstruction extractArgInsn = new GetElementInstruction(ArrayElementType.OBJECT); extractArgInsn.setArray(argumentsVar); extractArgInsn.setIndex(indexInsn.getReceiver()); extractArgInsn.setReceiver(program.createVariable()); add(extractArgInsn); arguments.add(unbox(extractArgInsn.getReceiver(), reflectMethod.method.parameterType(i))); } }
private boolean processProperty(MethodReader method, CallLocation callLocation, InvokeInstruction invoke) { if (isProperGetter(method)) { String propertyName = extractSuggestedPropertyName(method); if (propertyName == null) { propertyName = method.getName().charAt(0) == 'i' ? cutPrefix(method.getName(), 2) : cutPrefix(method.getName(), 3); } Variable result = invoke.getReceiver() != null ? program.createVariable() : null; addPropertyGet(propertyName, invoke.getInstance(), result, invoke.getLocation()); if (result != null) { result = marshaller.unwrapReturnValue(callLocation, result, method.getResultType()); copyVar(result, invoke.getReceiver(), invoke.getLocation()); } return true; } if (isProperSetter(method)) { String propertyName = extractSuggestedPropertyName(method); if (propertyName == null) { propertyName = cutPrefix(method.getName(), 3); } Variable wrapped = marshaller.wrapArgument(callLocation, invoke.getArguments().get(0), method.parameterType(0), false); addPropertySet(propertyName, invoke.getInstance(), wrapped, invoke.getLocation()); return true; } diagnostics.error(callLocation, "Method {{m0}} is not a proper native JavaScript property " + "declaration", invoke.getMethod()); return false; }