Skip to content

Commit

Permalink
Merge pull request #245 from stevehalliwell/remove-freeze
Browse files Browse the repository at this point in the history
Remove freeze opcode, now a stdlib func
  • Loading branch information
stevehalliwell authored Mar 21, 2024
2 parents 7e3557e + badb107 commit 499ec80
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 84 deletions.
43 changes: 43 additions & 0 deletions ulox/ulox.core.tests/ClassTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1739,5 +1739,48 @@ class CoffeeMaker {

Assert.AreEqual("Enjoy your cup of coffee and chicory", testEngine.InterpreterResult);
}

[Test]
public void InstanceFromClass_WhenFrozenAndNonExistingFieldWritten_ShouldPreventChangeAndLog()
{
testEngine.Run(@"
class Foo
{
}
var inst = Foo();
inst.a = 10;");

StringAssert.StartsWith("Attempted to create a new ", testEngine.InterpreterResult);
}

[Test]
public void InstanceFromClass_WhenHasInitAndInitChainAndNonExistingFieldWritten_ShouldPreventChangeAndLog()
{
testEngine.Run(@"
class Foo
{
var b = 2;
init(){this.c = 3;}
}
var inst = Foo();
inst.a = 10;");

StringAssert.StartsWith("Attempted to create a new ", testEngine.InterpreterResult);
}

[Test]
public void Class_WhenFrozenAndNonExistingFieldWritten_ShouldPreventChangeAndLog()
{
testEngine.Run(@"
class Foo
{
}
Foo.a = 10;");

StringAssert.StartsWith("Attempted to create a new ", testEngine.InterpreterResult);
}
}
}
60 changes: 14 additions & 46 deletions ulox/ulox.core.tests/FreezeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,6 @@ namespace ULox.Core.Tests
{
public class FreezeTests : EngineTestBase
{
[Test]
public void InstanceFromClass_WhenFrozenAndNonExistingFieldWritten_ShouldPreventChangeAndLog()
{
testEngine.Run(@"
class Foo
{
}
var inst = Foo();
inst.a = 10;");

StringAssert.StartsWith("Attempted to create a new ", testEngine.InterpreterResult);
}

[Test]
public void InstanceFromClass_WhenHasInitAndInitChainAndNonExistingFieldWritten_ShouldPreventChangeAndLog()
{
testEngine.Run(@"
class Foo
{
var b = 2;
init(){this.c = 3;}
}
var inst = Foo();
inst.a = 10;");

StringAssert.StartsWith("Attempted to create a new ", testEngine.InterpreterResult);
}

[Test]
public void Class_WhenFrozenAndNonExistingFieldWritten_ShouldPreventChangeAndLog()
{
testEngine.Run(@"
class Foo
{
}
freeze Foo;
Foo.a = 10;");

StringAssert.StartsWith("Attempted to create a new ", testEngine.InterpreterResult);
}

[Test]
public void Instance_WhenUnfrozen_ShouldActAsDynamic()
{
Expand All @@ -63,15 +19,27 @@ class Pair {}
Assert.AreEqual("3", testEngine.InterpreterResult);
}


[Test]
public void Freeze_WhenNumber_ShouldFail()
{
testEngine.Run(@"
var foo = 7;
freeze foo;");
Freeze(foo);");

StringAssert.StartsWith("Freeze attempted on unsupported type 'Double'", testEngine.InterpreterResult);
}

[Test]
public void Freeze_WhenDynamic_ShouldBeIsFrozen()
{
testEngine.Run(@"
var foo = {=};
print(IsFrozen(foo));
Freeze(foo);
print(IsFrozen(foo));
");

Assert.AreEqual("FalseTrue", testEngine.InterpreterResult);
}
}
}
2 changes: 1 addition & 1 deletion ulox/ulox.core.tests/uloxs/Samples/15-Freeze.ulox
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ print(IsFrozen(dynInst));

dynInst.a = 10;

freeze dynInst;
Freeze(dynInst);
print(IsFrozen(dynInst));


Expand Down
9 changes: 9 additions & 0 deletions ulox/ulox.core.tests/uloxs/Tests/Freeze.ulox
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,13 @@ testset FreezeTests

Assert.AreEqual(expected, result);
}

test FreezeDynamic
{
var inst = {=};

Freeze(inst);

expect IsFrozen(inst);
}
}
1 change: 0 additions & 1 deletion ulox/ulox.core/Package/Runtime/Compiler/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ private void Setup()
(TokenType.OPEN_BRACE, CompilerStatements.BlockStatement),
(TokenType.THROW, CompilerStatements.ThrowStatement),
(TokenType.END_STATEMENT, CompilerStatements.NoOpStatement),
(TokenType.FREEZE, CompilerStatements.FreezeStatement),
(TokenType.EXPECT, CompilerStatements.ExpectStatement),
(TokenType.MATCH, CompilerStatements.MatchStatement),
(TokenType.LABEL, CompilerStatements.LabelStatement),
Expand Down
7 changes: 0 additions & 7 deletions ulox/ulox.core/Package/Runtime/Compiler/CompilerStatements.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,6 @@ public static void NoOpStatement(Compiler compiler)
{
}

public static void FreezeStatement(Compiler compiler)
{
compiler.Expression();
compiler.EmitPacket(new ByteCodePacket(OpCode.FREEZE));
compiler.ConsumeEndStatement();
}

//todo expects be desugar
//could be come if (!(exp)) throw "Expects failed, '{msg}'"
//problem is we don't know what an exp or statement is yet, tokens would need to either be ast or know similar for
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace ULox
{
//todo can enums also be made entirely at compile time? or become sugar on a readonly dynamic?
public sealed class EnumTypeDeclarationCompliette : TypeCompilette
{
private enum Mode
Expand Down
24 changes: 0 additions & 24 deletions ulox/ulox.core/Package/Runtime/Engine/VM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -447,10 +447,6 @@ public InterpreterResult Run()
TestRunner.DoTestOpCode(this, chunk, packet.testOpDetails);
break;

case OpCode.FREEZE:
DoFreezeOp();
break;

case OpCode.NATIVE_TYPE:
DoNativeTypeOp(chunk, packet.NativeType);
break;
Expand Down Expand Up @@ -1045,26 +1041,6 @@ private void DuplicateStackValuesNew(int startAt, int count)
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void DoFreezeOp()
{
var instVal = Pop();
switch (instVal.type)
{
case ValueType.Instance:
instVal.val.asInstance.Freeze();
break;

case ValueType.UserType:
instVal.val.asClass.Freeze();
break;

default:
ThrowRuntimeException($"Freeze attempted on unsupported type '{instVal.type}'");
break;
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void DoGetPropertyOp(Chunk chunk, byte constantIndex, Value targetVal)
{
Expand Down
2 changes: 0 additions & 2 deletions ulox/ulox.core/Package/Runtime/Enums/OpCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ public enum OpCode : byte
SET_FIELD,
INVOKE,

FREEZE, //could collapse with READ_ONLY

TEST,

BUILD,
Expand Down
2 changes: 0 additions & 2 deletions ulox/ulox.core/Package/Runtime/Enums/TokenType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ public enum TokenType
STATIC,
INIT,

FREEZE,

MIXIN,

TEST_SET,
Expand Down
22 changes: 22 additions & 0 deletions ulox/ulox.core/Package/Runtime/Library/StdLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public Table GetBindings()
(nameof(str), Value.New(str, 1, 1)),
(nameof(IsFrozen), Value.New(IsFrozen, 1, 1)),
(nameof(Unfreeze), Value.New(Unfreeze, 1, 1)),
(nameof(Freeze), Value.New(Freeze, 1, 1)),
(nameof(GenerateStackDump), Value.New(GenerateStackDump, 1, 0)),
(nameof(GenerateGlobalsDump), Value.New(GenerateGlobalsDump, 1, 0))
);
Expand Down Expand Up @@ -220,6 +221,27 @@ public static NativeCallResult Unfreeze(Vm vm)
return NativeCallResult.SuccessfulExpression;
}

public static NativeCallResult Freeze(Vm vm)
{
var instVal = vm.GetArg(1);
switch (instVal.type)
{
case ValueType.Instance:
instVal.val.asInstance.Freeze();
break;

case ValueType.UserType:
instVal.val.asClass.Freeze();
break;

default:
vm.ThrowRuntimeException($"Freeze attempted on unsupported type '{instVal.type}'");
break;
}

return NativeCallResult.SuccessfulExpression;
}

public static NativeCallResult str(Vm vm)
{
var v = vm.GetArg(1);
Expand Down
1 change: 0 additions & 1 deletion ulox/ulox.core/Package/Runtime/Scanner/Scanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ private void Setup()
("static", TokenType.STATIC),
("init", TokenType.INIT),
("cname", TokenType.CONTEXT_NAME_CLASS),
("freeze", TokenType.FREEZE),

("typeof", TokenType.TYPEOF),

Expand Down

0 comments on commit 499ec80

Please sign in to comment.