ObjInstanceClass optimization
This commit is contained in:
parent
652e1d3af4
commit
88b355c40d
@ -769,12 +769,12 @@ class Compiler(
|
|||||||
for ((name, record) in objects) {
|
for ((name, record) in objects) {
|
||||||
when (record.visibility) {
|
when (record.visibility) {
|
||||||
Visibility.Public -> {
|
Visibility.Public -> {
|
||||||
thisObj.publicFields += name
|
thisObj.objClass.publicFields += name
|
||||||
thisObj.protectedFields += name
|
thisObj.objClass.protectedFields += name
|
||||||
}
|
}
|
||||||
|
|
||||||
Visibility.Protected ->
|
Visibility.Protected ->
|
||||||
thisObj.protectedFields += name
|
thisObj.objClass.protectedFields += name
|
||||||
|
|
||||||
Visibility.Private -> {
|
Visibility.Private -> {
|
||||||
//println("private field: $name")
|
//println("private field: $name")
|
||||||
|
@ -7,6 +7,9 @@ class ObjClass(
|
|||||||
vararg val parents: ObjClass,
|
vararg val parents: ObjClass,
|
||||||
) : Obj() {
|
) : Obj() {
|
||||||
|
|
||||||
|
internal val publicFields = mutableSetOf<String>()
|
||||||
|
internal val protectedFields = mutableSetOf<String>()
|
||||||
|
|
||||||
var instanceConstructor: Statement? = null
|
var instanceConstructor: Statement? = null
|
||||||
|
|
||||||
val allParentsSet: Set<ObjClass> = parents.flatMap {
|
val allParentsSet: Set<ObjClass> = parents.flatMap {
|
||||||
|
@ -2,18 +2,15 @@ package net.sergeych.lyng
|
|||||||
|
|
||||||
class ObjInstance(override val objClass: ObjClass) : Obj() {
|
class ObjInstance(override val objClass: ObjClass) : Obj() {
|
||||||
|
|
||||||
internal val publicFields = mutableSetOf<String>()
|
|
||||||
internal val protectedFields = mutableSetOf<String>()
|
|
||||||
|
|
||||||
internal lateinit var instanceContext: Context
|
internal lateinit var instanceContext: Context
|
||||||
|
|
||||||
override suspend fun readField(context: Context, name: String): ObjRecord {
|
override suspend fun readField(context: Context, name: String): ObjRecord {
|
||||||
return if( name in publicFields ) instanceContext[name]!!
|
return if( name in objClass.publicFields ) instanceContext[name]!!
|
||||||
else super.readField(context, name)
|
else super.readField(context, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun writeField(context: Context, name: String, newValue: Obj) {
|
override suspend fun writeField(context: Context, name: String, newValue: Obj) {
|
||||||
if( name in publicFields ) {
|
if( name in objClass.publicFields ) {
|
||||||
val f = instanceContext[name]!!
|
val f = instanceContext[name]!!
|
||||||
if( !f.isMutable ) ObjIllegalAssignmentError(context, "can't reassign val $name").raise()
|
if( !f.isMutable ) ObjIllegalAssignmentError(context, "can't reassign val $name").raise()
|
||||||
if( f.value.assign(context, newValue) == null)
|
if( f.value.assign(context, newValue) == null)
|
||||||
@ -23,12 +20,12 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun invokeInstanceMethod(context: Context, name: String, args: Arguments): Obj {
|
override suspend fun invokeInstanceMethod(context: Context, name: String, args: Arguments): Obj {
|
||||||
if( name in publicFields ) return instanceContext[name]!!.value.invoke(context, this, args)
|
if( name in objClass.publicFields ) return instanceContext[name]!!.value.invoke(context, this, args)
|
||||||
return super.invokeInstanceMethod(context, name, args)
|
return super.invokeInstanceMethod(context, name, args)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun toString(): String {
|
override fun toString(): String {
|
||||||
val fields = publicFields.map {
|
val fields = objClass.publicFields.map {
|
||||||
instanceContext[it]?.value?.toString() ?: "??"
|
instanceContext[it]?.value?.toString() ?: "??"
|
||||||
}.joinToString(", ")
|
}.joinToString(", ")
|
||||||
return "${objClass.className}($fields)"
|
return "${objClass.className}($fields)"
|
||||||
@ -37,7 +34,7 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
|
|||||||
override suspend fun compareTo(context: Context, other: Obj): Int {
|
override suspend fun compareTo(context: Context, other: Obj): Int {
|
||||||
if( other !is ObjInstance ) return -1
|
if( other !is ObjInstance ) return -1
|
||||||
if( other.objClass != objClass ) return -1
|
if( other.objClass != objClass ) return -1
|
||||||
for( f in publicFields ) {
|
for( f in objClass.publicFields ) {
|
||||||
val a = instanceContext[f]!!.value
|
val a = instanceContext[f]!!.value
|
||||||
val b = other.instanceContext[f]!!.value
|
val b = other.instanceContext[f]!!.value
|
||||||
val d = a.compareTo(context, b)
|
val d = a.compareTo(context, b)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user