Skip to content
4 changes: 2 additions & 2 deletions javatools/src/main/java/org/xvm/api/InterpreterConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,15 @@ public void invoke0(MethodStructure method, String... asArg) {
TypeConstant typeArg = method.getParam(0).getType();

assert typeStrings.isA(typeArg);
ahArg = new ObjectHandle[]{xString.makeArrayHandle(asArg)};
ahArg = new ObjectHandle[]{xString.makeArrayHandle(m_containerMain, asArg)};
}
break;

case 1: {
TypeConstant typeArg = method.getParam(0).getType();
assert typeStrings.isA(typeArg);
// the method requires an array that we can supply
ahArg = new ObjectHandle[]{xString.makeArrayHandle(asArg)};
ahArg = new ObjectHandle[]{xString.makeArrayHandle(m_containerMain, asArg)};
break;
}
}
Expand Down
3 changes: 2 additions & 1 deletion javatools/src/main/java/org/xvm/asm/OpVar.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ protected TypeComposition getArrayClass(Frame frame, TypeConstant typeList) {
if (clzArray == null || !typeList.equals(typePrev)) {
TypeConstant typeEl = typeList.resolveGenericType("Element");

clzArray = xArray.INSTANCE.ensureParameterizedClass(context.f_container, typeEl);
clzArray = context.f_container.nativeTemplate(xArray.class).
ensureParameterizedClass(context.f_container, typeEl);

context.setOpInfo(this, Category.Composition, clzArray);
context.setOpInfo(this, Category.Type, typeList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7812,7 +7812,7 @@ public TypeHandle ensureTypeHandle(Container container) {
}

// don't cache a "foreign" handle
return xRTType.makeForeignHandle(this);
return xRTType.makeForeignHandle(container, this);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion javatools/src/main/java/org/xvm/asm/op/Assert.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ protected int complete(Frame frame, int iPC, String sMsg) {
ClassConstant constClz = (ClassConstant) idConstruct.getNamespace();
ClassTemplate template = frame.ensureTemplate(constClz);
ClassComposition clzTarget = template.getCanonicalClass(frame.f_context.f_container);
StringHandle hMsg = xString.makeHandle(sMsg);
StringHandle hMsg = xString.makeHandle(frame, sMsg);
ObjectHandle[] ahArg = new ObjectHandle[construct.getMaxVars()];

ahArg[0] = hMsg;
Expand Down
6 changes: 4 additions & 2 deletions javatools/src/main/java/org/xvm/asm/op/MoveRef.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.xvm.javajit.registers.Ref;

import org.xvm.runtime.Container;
import org.xvm.runtime.Frame;
import org.xvm.runtime.ObjectHandle;
import org.xvm.runtime.TypeComposition;
Expand Down Expand Up @@ -71,8 +72,9 @@ public int process(Frame frame, int iPC) {
typeReg = infoSrc.getType();
}
} else {
TypeComposition clzRef = xRef.INSTANCE.ensureParameterizedClass(
frame.f_context.f_container, infoSrc.getType());
Container container = frame.container();
TypeComposition clzRef = container.nativeTemplate(xRef.class).
ensureParameterizedClass(container, infoSrc.getType());
hRef = new RefHandle(clzRef, frame, m_nFromValue);
typeReg = hRef.getType();
}
Expand Down
6 changes: 4 additions & 2 deletions javatools/src/main/java/org/xvm/asm/op/MoveVar.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.xvm.javajit.registers.Ref;

import org.xvm.runtime.Container;
import org.xvm.runtime.Frame;
import org.xvm.runtime.ObjectHandle;
import org.xvm.runtime.ObjectHandle.ExceptionHandle;
Expand Down Expand Up @@ -103,8 +104,9 @@ protected int complete(Frame frame, ObjectHandle hReferent) {
? frame.poolContext().typeObject()
: frame.getVarInfo(nFrom).getType();
}
TypeComposition clzRef = xVar.INSTANCE.
ensureParameterizedClass(frame.f_context.f_container, typeReferent);
Container container = frame.container();
TypeComposition clzRef = container.nativeTemplate(xVar.class).
ensureParameterizedClass(container, typeReferent);
hRef = new RefHandle(clzRef, frame, nFrom);
if (fNextReg) {
frame.introduceResolvedVar(nTo, hRef.getType());
Expand Down
5 changes: 3 additions & 2 deletions javatools/src/main/java/org/xvm/asm/op/Var_M.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,9 @@ public int process(Frame frame, int iPC) {

frame.introduceResolvedVar(m_nVar, typeMap, null, Frame.VAR_STANDARD, null);

return xListMap.INSTANCE.constructMap(frame, typeMap, ahKey, ahValue,
anyDeferred(ahKey), anyDeferred(ahValue), m_nVar);
return frame.nativeTemplate(xListMap.class).
constructMap(frame, typeMap, ahKey, ahValue,
anyDeferred(ahKey), anyDeferred(ahValue), m_nVar);
} catch (ExceptionHandle.WrapperException e) {
return frame.raiseException(e);
}
Expand Down
5 changes: 3 additions & 2 deletions javatools/src/main/java/org/xvm/asm/op/Var_MN.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,9 @@ public int process(Frame frame, int iPC) {
frame.introduceResolvedVar(m_nVar, typeMap,
frame.getString(m_nNameId), Frame.VAR_STANDARD, null);

return xListMap.INSTANCE.constructMap(frame, typeMap, ahKey, ahValue,
anyDeferred(ahKey), anyDeferred(ahValue), m_nVar);
return frame.nativeTemplate(xListMap.class).
constructMap(frame, typeMap, ahKey, ahValue,
anyDeferred(ahKey), anyDeferred(ahValue), m_nVar);
} catch (ExceptionHandle.WrapperException e) {
return frame.raiseException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ public StringHandle[] getFieldNameArray() {
FieldInfo field = entry.getValue();

if (!(enid instanceof NestedIdentity) && field.isRegular()) {
ashNames[i++] = xString.makeHandle(field.getName());
ashNames[i++] = xString.makeHandle(f_container, field.getName());
}
}
assert i == m_cRegularFields;
Expand Down
48 changes: 45 additions & 3 deletions javatools/src/main/java/org/xvm/runtime/ClassTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,51 @@ public void registerNativeTemplates() {
* @param template the new template
*/
protected void registerNativeTemplate(ClassTemplate template) {
registerAuxiliaryTemplate(template);

// this template implements a composite type declared by "this" one, so it has no name of
// its own to be found under; publish it into the container's native template table
nativeTemplates().register(template);
}

/**
* Register a native template that serves an additional structure using a template class that
* is already spoken for. Unlike {@link #registerNativeTemplate}, this does not make the
* template its class's native instance - {@link NativeTemplates#get} must keep answering with
* the template registered for the class's own structure.
*
* @param template the new template
*/
protected void registerAuxiliaryTemplate(ClassTemplate template) {
((NativeContainer) f_container).registerNativeTemplate(template.getCanonicalType(), template);
}

/**
* Obtain this template's container's table of native templates - for registering a template,
* or anything else that works on the table itself. To just look one up, prefer the shorthand
* {@link Container#nativeTemplate} on {@link #f_container}.
*
* @return this template's container's table of native templates
*/
protected NativeTemplates nativeTemplates() {
return f_container.nativeTemplates();
}

/**
* Note: the class has to be named explicitly, because it is the class that <i>declares</i> the
* calling method that matters, not this template's own class. A template that inherits such a
* method - {@code xVar} extends {@code xRef} - is the native instance of its own class while
* still not being the one the inherited method is asking about.
*
* @param clzTemplate the native template class being asked about
*
* @return true iff this is the container's native template for the specified class, rather
* than one of the per-structure templates that the same class also backs
*/
protected boolean isNativeInstance(Class<? extends ClassTemplate> clzTemplate) {
return this == f_container.nativeTemplate(clzTemplate);
}

/**
* Initialize native properties, methods and functions.
*/
Expand Down Expand Up @@ -180,7 +222,7 @@ public ClassTemplate getSuper() {
if ("Object".equals(f_sName)) {
return null;
}
templateSuper = m_templateSuper = xObject.INSTANCE;
templateSuper = m_templateSuper = f_container.nativeTemplate(xObject.class);
} else {
templateSuper = m_templateSuper =
f_container.getTemplate(f_structSuper.getIdentityConstant());
Expand Down Expand Up @@ -1970,7 +2012,7 @@ public int overflow(Frame frame) {
* @return one of the {@link Op#R_NEXT}, {@link Op#R_CALL} or {@link Op#R_EXCEPTION} values
*/
protected int buildStringValue(Frame frame, ObjectHandle hTarget, int iReturn) {
return frame.assignValue(iReturn, xString.makeHandle(hTarget.toString()));
return frame.assignValue(iReturn, xString.makeHandle(frame, hTarget.toString()));
}


Expand Down Expand Up @@ -2427,7 +2469,7 @@ private void prepareFinalizer(Frame frame, MethodStructure ctor, ObjectHandle[]
if (hfn == null) {
// in case super constructors have their own finalizers, we need a non-null anchor
// that may be replaced by Frame.chainFinalizers()
hfn = FullyBoundHandle.NO_OP;
hfn = FullyBoundHandle.ensureNoOp(frame.container());
}

frame.m_hfnFinally = hfn;
Expand Down
34 changes: 31 additions & 3 deletions javatools/src/main/java/org/xvm/runtime/Container.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,12 @@ public ServiceContext ensureServiceContext() {
ServiceContext ctx = m_contextMain;
if (ctx == null) {
try (var ignore = ConstantPool.withPool(getConstantPool())) {
xService templateService = nativeTemplate(xService.class);

m_contextMain = ctx = createServiceContext(getModule().getName());
xService.INSTANCE.createServiceHandle(ctx,
xService.INSTANCE.getCanonicalClass(),
xService.INSTANCE.getCanonicalType());
templateService.createServiceHandle(ctx,
templateService.getCanonicalClass(),
templateService.getCanonicalType());
}
}
return ctx;
Expand Down Expand Up @@ -399,6 +401,32 @@ public Container getOriginContainer(SingletonConstant constSingle) {
: this;
}

/**
* Obtain this container's table of native templates.
*
* <p>One of three ways in. This one is the table itself, for bulk work - registering a
* template, or anything else that is not a single lookup. To look one template up, use
* {@link #nativeTemplate}. To get the table from something that is not a container -
* a frame, a template, a composition - use {@link NativeTemplates#of}.</p>
*
* @return this container's table of native templates
*/
public NativeTemplates nativeTemplates() {
return getNativeContainer().f_templates;
}

/**
* Look one native template up - the shorthand for the common case, over
* {@link #nativeTemplates()}.
*
* @param clzTemplate the native template class
*
* @return this container's native template of that class
*/
public <T extends ClassTemplate> T nativeTemplate(Class<T> clzTemplate) {
return nativeTemplates().get(clzTemplate);
}

/**
* @return a ClassTemplate for a type associated with the specified name (core classes only)
*/
Expand Down
25 changes: 22 additions & 3 deletions javatools/src/main/java/org/xvm/runtime/Frame.java
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public Frame createNativeFrame(Op[] aop, ObjectHandle[] ahVar, int iReturn, int[
* @return a new frame
*/
public Frame createWaitFrame(CompletableFuture<ObjectHandle> cfResult, int iReturn) {
return createWaitFrame(xFuture.makeHandle(cfResult), iReturn);
return createWaitFrame(xFuture.makeHandle(container(), cfResult), iReturn);
}

/**
Expand Down Expand Up @@ -280,7 +280,7 @@ public Frame createWaitFrame(CompletableFuture<ObjectHandle[]> cfResult, int[] a
CompletableFuture<ObjectHandle> cfReturn =
cfResult.thenApply(ahResult -> ahResult[iResult]);

ahFuture[i] = xFuture.makeHandle(cfReturn);
ahFuture[i] = xFuture.makeHandle(container(), cfReturn);

frameNext.f_aInfo[i] = new VarInfo(xFuture.TYPE, VAR_DYNAMIC_REF);
}
Expand Down Expand Up @@ -1047,7 +1047,7 @@ public int assignFutureResult(int iReturn, CompletableFuture<ObjectHandle> cfRes
}

if (isFuture(iReturn)) {
return assignValue(iReturn, xFuture.makeHandle(cfResult));
return assignValue(iReturn, xFuture.makeHandle(container(), cfResult));
}

// the wait frame will deal with exceptions
Expand Down Expand Up @@ -1331,6 +1331,25 @@ public ConstantPool poolContext() {
return f_context.f_pool;
}

/**
* @return the container that owns the service this frame is running on
*/
public Container container() {
return f_context.f_container;
}

/**
* Look one native template up - the shorthand for the common case, over
* {@code container().nativeTemplates()}.
*
* @param clzTemplate the native template class
*
* @return this frame's container's native template of that class
*/
public <T extends ClassTemplate> T nativeTemplate(Class<T> clzTemplate) {
return container().nativeTemplate(clzTemplate);
}

/**
* @return an ObjectHandle for the constant (could be a DeferredCallHandle)
*/
Expand Down
8 changes: 4 additions & 4 deletions javatools/src/main/java/org/xvm/runtime/MainContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ public ObjectHandle getInjectable(Frame frame, String sName, TypeConstant type,
if (!listValue.isEmpty()) {
if (typeRequired.equals(typeString)) {
// require String, return the last element
return xString.makeHandle(listValue.getLast());
return xString.makeHandle(frame, listValue.getLast());
}
if (typeRequired.equals(typeStrings)) {
// require String[], return the whole List<String> as an array
String[] asValue = listValue.toArray(String[]::new);
return xString.makeArrayHandle(asValue);
return xString.makeArrayHandle(frame.container(), asValue);
}
if (typeRequired.isEnum()) {
TypeComposition clz = typeRequired.ensureClass(frame);
Expand Down Expand Up @@ -108,7 +108,7 @@ private ObjectHandle toDestringable(Frame frame, TypeConstant type, String sValu
TypeComposition clz = type.ensureClass(frame);
ClassTemplate template = clz.getTemplate();
MethodStructure ctor = template.getStructure().findMethod("construct", 1, pool.typeString());
ObjectHandle[] ahArgs = new ObjectHandle[]{xString.makeHandle(sValue)};
ObjectHandle[] ahArgs = new ObjectHandle[]{xString.makeHandle(frame, sValue)};
int iResult = template.construct(frame, ctor, clz, null, ahArgs, Op.A_STACK);
return frame.popResult(iResult);
}
Expand Down Expand Up @@ -203,7 +203,7 @@ public void invoke0(String sMethodName, ObjectHandle... ahArg) {
CallChain chain = clzModule.getMethodCallChain(sigMethod);
boolean fReturn = sigMethod.getReturnCount() > 0;

FunctionHandle hInstantiateModuleAndRun = new NativeFunctionHandle((frame, ah, iRet) -> {
FunctionHandle hInstantiateModuleAndRun = new NativeFunctionHandle(this, (frame, ah, iRet) -> {
SingletonConstant idModule = frame.poolContext().ensureSingletonConstConstant(f_idModule);
ObjectHandle hModule = frame.getConstHandle(idModule);
int iReturn = fReturn ? Op.A_STACK : Op.A_IGNORE;
Expand Down
Loading
Loading