Skip to content

Commit

Permalink
* Remove redundant message for RuntimeHelper.requiresNonNull
Browse files Browse the repository at this point in the history
* Address review comments
  • Loading branch information
mcimadamore committed Feb 15, 2021
1 parent 8d424ba commit 1f3847b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
Expand Up @@ -154,8 +154,8 @@ Constant emitGetter(JavaSourceBuilder builder, String mods, Function<List<String
return this;
}

Constant emitGetter(JavaSourceBuilder builder, String mods, Function<List<String>, String> getterNameFunc, String msgIfNull) {
builder.emitGetter(mods, kind.type, getterNameFunc.apply(getterNameParts()), accessExpression(), true, msgIfNull);
Constant emitGetter(JavaSourceBuilder builder, String mods, Function<List<String>, String> getterNameFunc, String symbolName) {
builder.emitGetter(mods, kind.type, getterNameFunc.apply(getterNameParts()), accessExpression(), true, symbolName);
return this;
}

Expand All @@ -176,6 +176,10 @@ public Constant emitIfAbsent(String name, Constant.Kind kind, Supplier<Constant>
Constant constant = namesGenerated.get(lookupName);
if (constant == null) {
constant = constantFactory.get();
if (constant.kind != kind) {
throw new AssertionError("Factory return wrong kind of constant; expected: "
+ kind + "; found: " + constant.kind);
}
namesGenerated.put(lookupName, constant);
}
return constant;
Expand Down Expand Up @@ -263,15 +267,15 @@ private void emitLayoutString(MemoryLayout l) {
}
emitLayoutString(((SequenceLayout) l).elementLayout());
append(")");
} else if (l instanceof GroupLayout) {
if (((GroupLayout) l).isStruct()) {
} else if (l instanceof GroupLayout group) {
if (group.isStruct()) {
append("MemoryLayout.ofStruct(\n");
} else {
append("MemoryLayout.ofUnion(\n");
}
incrAlign();
String delim = "";
for (MemoryLayout e : ((GroupLayout) l).memberLayouts()) {
for (MemoryLayout e : group.memberLayouts()) {
append(delim);
indent();
emitLayoutString(e);
Expand Down
Expand Up @@ -64,7 +64,7 @@ public void addVar(String javaName, String nativeName, MemoryLayout layout, Clas
if (type.equals(MemorySegment.class)) {
emitWithConstantClass(javaName, constantBuilder -> {
constantBuilder.addSegment(javaName, nativeName, layout)
.emitGetter(this, MEMBER_MODS, Constant.QUALIFIED_NAME, "unresolved symbol: " + nativeName);
.emitGetter(this, MEMBER_MODS, Constant.QUALIFIED_NAME, nativeName);
});
} else {
emitWithConstantClass(javaName, constantBuilder -> {
Expand All @@ -73,7 +73,7 @@ public void addVar(String javaName, String nativeName, MemoryLayout layout, Clas
Constant vhConstant = constantBuilder.addGlobalVarHandle(javaName, nativeName, layout, type)
.emitGetter(this, MEMBER_MODS, Constant.QUALIFIED_NAME);
Constant segmentConstant = constantBuilder.addSegment(javaName, nativeName, layout)
.emitGetter(this, MEMBER_MODS, Constant.QUALIFIED_NAME, "unresolved symbol: " + nativeName);
.emitGetter(this, MEMBER_MODS, Constant.QUALIFIED_NAME, nativeName);
emitGlobalGetter(segmentConstant, vhConstant, javaName, nativeName, type);
emitGlobalSetter(segmentConstant, vhConstant, javaName, nativeName, type);
});
Expand All @@ -84,7 +84,7 @@ public void addVar(String javaName, String nativeName, MemoryLayout layout, Clas
public void addFunction(String javaName, String nativeName, MethodType mtype, FunctionDescriptor desc, boolean varargs, List<String> paramNames) {
emitWithConstantClass(javaName, constantBuilder -> {
Constant mhConstant = constantBuilder.addMethodHandle(javaName, nativeName, mtype, desc, varargs)
.emitGetter(this, MEMBER_MODS, Constant.QUALIFIED_NAME, "unresolved symbol: " + nativeName);
.emitGetter(this, MEMBER_MODS, Constant.QUALIFIED_NAME, nativeName);
emitFunctionWrapper(mhConstant, javaName, nativeName, mtype, varargs, paramNames);
});
}
Expand Down Expand Up @@ -154,7 +154,7 @@ private void emitFunctionWrapper(Constant mhConstant, String javaName, String na
indent();
append("var mh$ = RuntimeHelper.requireNonNull(");
append(mhConstant.accessExpression());
append(", \"unresolved symbol: ");
append(", \"");
append(nativeName);
append("\");\n");
indent();
Expand Down Expand Up @@ -244,7 +244,7 @@ private void emitGlobalGetter(Constant segmentConstant, Constant vhConstant, Str
append(vhConstant.accessExpression());
append(".get(RuntimeHelper.requireNonNull(");
append(segmentConstant.accessExpression());
append(", \"unresolved symbol: ");
append(", \"");
append(nativeName);
append("\"));\n");
decrAlign();
Expand All @@ -262,7 +262,7 @@ private void emitGlobalSetter(Constant segmentConstant, Constant vhConstant, Str
append(vhConstant.accessExpression());
append(".set(RuntimeHelper.requireNonNull(");
append(segmentConstant.accessExpression());
append(", \"unresolved symbol: ");
append(", \"");
append(nativeName);
append("\"), x);\n");
decrAlign();
Expand Down
Expand Up @@ -229,7 +229,7 @@ protected void emitImportSection() {
append(".*;\n");
}

protected void emitGetter(String mods, Class<?> type, String name, String access, boolean nullCheck, String errMsg) {
protected void emitGetter(String mods, Class<?> type, String name, String access, boolean nullCheck, String symbolName) {
incrAlign();
indent();
append(mods + " " + type.getSimpleName() + " " +name + "() {\n");
Expand All @@ -242,7 +242,7 @@ protected void emitGetter(String mods, Class<?> type, String name, String access
append(access);
if (nullCheck) {
append(",\"");
append(errMsg);
append(symbolName);
append("\")");
}
append(";\n");
Expand All @@ -257,6 +257,7 @@ protected void emitGetter(String mods, Class<?> type, String name, String access
}

int constant_counter = 0;
int constant_class_index = 0;

static final int CONSTANTS_PER_CLASS = Integer.getInteger("jextract.constants.per.class", 5);
ConstantBuilder constantBuilder;
Expand All @@ -267,7 +268,7 @@ protected void emitWithConstantClass(String javaName, Consumer<ConstantBuilder>
constantBuilder.classEnd();
}
constant_counter = 0;
constantBuilder = new ConstantBuilder(this, Kind.CLASS, javaName + "_constants");
constantBuilder = new ConstantBuilder(this, Kind.CLASS, "constants$" + constant_class_index++);
constantBuilder.classBegin();
}
constantConsumer.accept(constantBuilder);
Expand Down
Expand Up @@ -26,9 +26,9 @@ final class RuntimeHelper {
private final static ClassLoader LOADER = RuntimeHelper.class.getClassLoader();
private final static MethodHandles.Lookup MH_LOOKUP = MethodHandles.lookup();

static <T> T requireNonNull(T obj, String msg) {
static <T> T requireNonNull(T obj, String symbolName) {
if (obj == null) {
throw new UnsatisfiedLinkError(msg);
throw new UnsatisfiedLinkError("unresolved symbol: " + symbolName);
}
return obj;
}
Expand Down

0 comments on commit 1f3847b

Please sign in to comment.