Skip to content

Commit

Permalink
Improve Emscripten backend.
Browse files Browse the repository at this point in the history
  • Loading branch information
tritao committed Oct 30, 2023
1 parent 714f52e commit 49538e3
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 19 deletions.
18 changes: 18 additions & 0 deletions src/Generator/Generators/Emscripten/EmscriptenSources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,18 @@ public override void VisitClassConstructors(IEnumerable<Method> ctors)
}
}

public override bool VisitProperty(Property property)
{
if (property.Field != null)
{
return VisitFieldDecl(property.Field);
}

var @class = property.Namespace as Class;
WriteLineIndent($".property(\"{property.Name}\", &{property.GetMethod.QualifiedOriginalName})");
return true;
}

public override bool VisitMethodDecl(Method method)
{
Indent();
Expand All @@ -127,6 +139,12 @@ public override bool VisitMethodDecl(Method method)

public override bool VisitFieldDecl(Field field)
{
if (!field.Class.IsValueType)
{
Console.WriteLine($"Ignoring field for non value type class: {field.Class.QualifiedOriginalName}::{field.OriginalName})");
return false;
}

WriteLineIndent($".field(\"{field.Name}\", &{field.Class.QualifiedOriginalName}::{field.OriginalName})");
return true;
}
Expand Down
12 changes: 8 additions & 4 deletions src/Generator/Passes/GetterSetterToPropertyPass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,10 +341,14 @@ private static string GetPropertyNameFromSetter(string name)
return nameBuilder.ToString();
}

private bool IsGetter(Method method) =>
!method.IsDestructor &&
!method.OriginalReturnType.Type.IsPrimitiveType(PrimitiveType.Void) &&
method.Parameters.All(p => p.Kind == ParameterKind.IndirectReturnType);
private bool IsGetter(Method method)
{
if (Options.GeneratorKind == GeneratorKind.Emscripten && !method.IsConst)
return false;
return !method.IsDestructor &&
!method.OriginalReturnType.Type.IsPrimitiveType(PrimitiveType.Void) &&
method.Parameters.All(p => p.Kind == ParameterKind.IndirectReturnType);
}

private static bool IsSetter(Method method)
{
Expand Down
1 change: 1 addition & 0 deletions tests/Builtins.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
void ReturnsVoid () { }

std::nullptr_t ReturnsNullptr () { return nullptr; }
void PassNullptr (std::nullptr_t t) { }
std::nullptr_t PassAndReturnsNullptr (std::nullptr_t t) { return t; }

bool ReturnsBool () { return true; }
Expand Down
37 changes: 22 additions & 15 deletions tests/emscripten/test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ function builtins() {
eq(test.ReturnsBool(), true)
eq(test.PassAndReturnsBool(false), false)

// TODO: https://github.com/emscripten-core/emscripten/pull/18661
/*
eq(test.ReturnsNullptr(), null)
eq(test.PassNullptr(null), undefined)
eq(test.PassAndReturnsNullptr(null), null)
*/

eq(test.ReturnsChar(), ascii('a'));
eq(test.ReturnsSChar(), ascii('a'));
Expand All @@ -41,12 +45,6 @@ function builtins() {
eq(test.ReturnsInt32(), -5);
eq(test.ReturnsUInt32(), 5);

// TODO:
// https://github.com/WebAssembly/proposals/issues/7
// https://github.com/emscripten-core/emscripten/issues/11140
//eq(test.ReturnsInt64(), -5n);
//eq(test.ReturnsUInt64(), 5n);

const int8 = { min: -(2 ** 7), max: (2 ** 7) - 1 };
eq(test.PassAndReturnsInt8(int8.min), int8.min);
eq(test.PassAndReturnsInt8(int8.max), int8.max);
Expand All @@ -71,13 +69,21 @@ function builtins() {
eq(test.PassAndReturnsUInt32(uint32.min), uint32.min);
eq(test.PassAndReturnsUInt32(uint32.max), uint32.max);

//const int64 = { min: BigInt(2 ** 63) * -1n, max: BigInt(2 ** 63) - 1n };
//eq(test.PassAndReturnsInt64(int64.min), int64.min);
//eq(test.PassAndReturnsInt64(int64.max), int64.max);

//const uint64 = { min: BigInt(0), max: BigInt(2 ** 64) - 1n };
//eq(test.PassAndReturnsUInt64(uint64.min), uint64.min);
//eq(test.PassAndReturnsUInt64(uint64.max), uint64.max);
// TODO:
// https://github.com/WebAssembly/proposals/issues/7
// https://github.com/emscripten-core/emscripten/issues/11140
/*
eq(test.ReturnsInt64(), -5n);
eq(test.ReturnsUInt64(), 5n);
const int64 = { min: BigInt(2 ** 63) * -1n, max: BigInt(2 ** 63) - 1n };
eq(test.PassAndReturnsInt64(int64.min), int64.min);
eq(test.PassAndReturnsInt64(int64.max), int64.max);
const uint64 = { min: BigInt(0), max: BigInt(2 ** 64) - 1n };
eq(test.PassAndReturnsUInt64(uint64.min), uint64.min);
eq(test.PassAndReturnsUInt64(uint64.max), uint64.max);
*/
}

function enums() {
Expand Down Expand Up @@ -105,9 +111,10 @@ function classes() {

var classWithField = new test.ClassWithField();
eq(classWithField.ReturnsField(), 10);
eq(classWithField.Field, 10);
}

// NOTE: Field properties are only supported for value types
//eq(classWithField.Field, 10);
}

builtins();
enums();
Expand Down

0 comments on commit 49538e3

Please sign in to comment.