Skip to content

Commit

Permalink
synthesized property is not PHP visible + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubmisek committed Apr 28, 2024
1 parent 6acefee commit d5db31e
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 24 deletions.
Expand Up @@ -690,7 +690,10 @@ PropertySymbol SynthesizeProperty(Emit.PEModuleBuilder module, DiagnosticBag dia
// TODO: override base property?

//
var property = new SynthesizedPropertySymbol(this, propertyName, getter.IsStatic, type, getter.DeclaredAccessibility, getter, setter);
var property = new SynthesizedPropertySymbol(this, propertyName, getter.IsStatic, type, getter.DeclaredAccessibility,
getter, setter,
phphidden: true
);

module.SynthesizedManager.AddProperty(this, property);

Expand Down
Expand Up @@ -18,7 +18,20 @@ internal class SynthesizedPropertySymbol : PropertySymbol
readonly bool _isStatic;
readonly TypeSymbol _type;

public SynthesizedPropertySymbol(NamedTypeSymbol containing, string name, bool isStatic, TypeSymbol type, Accessibility accessibility, MethodSymbol getter, MethodSymbol setter)
internal override IEnumerable<AttributeData> GetCustomAttributesToEmit(CommonModuleCompilationState compilationState)
{
if (IsPhpHidden)
{
// [PhpHiddenAttribute]
yield return new SynthesizedAttributeData(
DeclaringCompilation.CoreMethods.Ctors.PhpHiddenAttribute,
ImmutableArray<TypedConstant>.Empty,
ImmutableArray<KeyValuePair<string, TypedConstant>>.Empty
);
}
}

public SynthesizedPropertySymbol(NamedTypeSymbol containing, string name, bool isStatic, TypeSymbol type, Accessibility accessibility, MethodSymbol getter, MethodSymbol setter, bool phphidden = false)
{
_containing = containing;
_name = name;
Expand All @@ -27,8 +40,12 @@ public SynthesizedPropertySymbol(NamedTypeSymbol containing, string name, bool i
_getMethod = getter;
_type = type;
_isStatic = isStatic;

this.IsPhpHidden = phphidden;
}

public bool IsPhpHidden { get; }

public override Symbol ContainingSymbol => _containing;

public override Accessibility DeclaredAccessibility => _accessibility;
Expand Down
83 changes: 61 additions & 22 deletions src/Tests/Peachpie.Runtime.Tests/ClrFeaturesTests.cs
@@ -1,8 +1,10 @@
using System;
using System.IO;
using System.Text;
using ComponentAce.Compression.Libs.zlib;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Pchp.Core;
using Pchp.Core.Reflection;

namespace Peachpie.Runtime.Tests
{
Expand All @@ -11,30 +13,35 @@ public class ClrFeaturesTests
{
string CompileAndRun(string code)
{
var outputStream = new MemoryStream();

using (var ctx = Context.CreateEmpty())
{
// mimic the execution in the given folder
ctx.RootPath = ctx.WorkingDirectory = Directory.GetCurrentDirectory();

// redirect text output
ctx.Output = new StreamWriter(outputStream, Encoding.UTF8) { AutoFlush = true };
ctx.OutputStream = outputStream;

// Compile and load
var script = Context.DefaultScriptingProvider.CreateScript(new Context.ScriptOptions()
{
Context = ctx,
IsSubmission = false,
//EmitDebugInformation = true,
Location = new Location(Path.GetFullPath("dummy.php"), 0, 0),
//AdditionalReferences = AdditionalReferences,
}, code);

// run
script.Evaluate(ctx, ctx.Globals, null);
return CompileAndRun(ctx, code);
}
}

string CompileAndRun(Context ctx, string code)
{
var outputStream = new MemoryStream();

// mimic the execution in the given folder
ctx.RootPath = ctx.WorkingDirectory = Directory.GetCurrentDirectory();

// redirect text output
ctx.Output = new StreamWriter(outputStream, Encoding.UTF8) { AutoFlush = true };
ctx.OutputStream = outputStream;

// Compile and load
var script = Context.DefaultScriptingProvider.CreateScript(new Context.ScriptOptions()
{
Context = ctx,
IsSubmission = false,
//EmitDebugInformation = true,
Location = new Location(Path.GetFullPath("dummy.php"), 0, 0),
//AdditionalReferences = AdditionalReferences,
}, code);

// run
script.Evaluate(ctx, ctx.Globals, null);

//
outputStream.Position = 0;
Expand All @@ -46,7 +53,7 @@ public void ScriptingProvider()
{
Assert.AreEqual("ok", CompileAndRun("<?php echo 'ok';"));
}

[TestMethod]
public void Dictionary()
{
Expand All @@ -72,5 +79,37 @@ public void Dictionary()
if (isset($d['key'])) echo 'ok';
"));
}

[TestMethod]
public void Property()
{
using (var ctx = Context.CreateEmpty())
{
CompileAndRun(@"<?php
class C {
function get_MyProperty() { return 1; }
function set_MyProperty($value) {}
function get_ValueOnly(): int { return 2; }
function get_StringOnly(): \System\String { return 'Hello'; }
}");

var c = ctx.Create("C");

var c_phptype = c.GetPhpTypeInfo();
Assert.IsNull(c_phptype.GetDeclaredProperty("MyProperty")); // not visible as PHP property

var c_type = c.GetType();
Assert.IsNotNull(c_type.GetProperty("MyProperty")); // visible as CLR property

var valueprop = c_type.GetProperty("ValueOnly");
Assert.IsNotNull(valueprop);
Assert.AreEqual(2L, valueprop.GetValue(c));

Assert.AreEqual("Hello", (string)c_type.GetProperty("StringOnly").GetValue(c));
}
}
}
}

0 comments on commit d5db31e

Please sign in to comment.