Browse Source

Fix QuickJS primitive type marshaling to take target sizes into account.

pull/1865/head
Joao Matos 11 months ago
parent
commit
642379a544
  1. 40
      src/Generator/Generators/QuickJS/QuickJSMarshal.cs
  2. 2
      src/Generator/Generators/QuickJS/QuickJSSources.cs
  3. 11
      src/Generator/Generators/QuickJS/QuickJSTypeCheckGen.cs

40
src/Generator/Generators/QuickJS/QuickJSMarshal.cs

@ -1,6 +1,7 @@ @@ -1,6 +1,7 @@
using System;
using CppSharp.AST;
using CppSharp.AST.Extensions;
using CppSharp.Extensions;
using CppSharp.Generators.C;
using CppSharp.Generators.CLI;
using CppSharp.Types;
@ -132,6 +133,9 @@ namespace CppSharp.Generators.Cpp @@ -132,6 +133,9 @@ namespace CppSharp.Generators.Cpp
var retName = Generator.GeneratedIdentifier(Context.ReturnVarName);
Context.Before.Write($"JSValue {retName} = ");
(uint width, uint _alignment) =
primitive.GetInfo(Context.Context.TargetInfo, out bool _signed);
switch (primitive)
{
case PrimitiveType.Void:
@ -150,13 +154,22 @@ namespace CppSharp.Generators.Cpp @@ -150,13 +154,22 @@ namespace CppSharp.Generators.Cpp
case PrimitiveType.UChar:
case PrimitiveType.Short:
case PrimitiveType.UShort:
Context.Before.WriteLine($"JS_NewInt32(ctx, {Context.ArgName});");
break;
case PrimitiveType.Int:
case PrimitiveType.Long:
if (width == 64)
Context.Before.WriteLine($"JS_NewBigInt64(ctx, {Context.ArgName});");
else
Context.Before.WriteLine($"JS_NewInt32(ctx, {Context.ArgName});");
break;
case PrimitiveType.UInt:
case PrimitiveType.ULong:
if (width == 64)
Context.Before.WriteLine($"JS_NewBigUint64(ctx, {Context.ArgName});");
else
Context.Before.WriteLine($"JS_NewUint32(ctx, {Context.ArgName});");
break;
@ -486,6 +499,9 @@ namespace CppSharp.Generators.Cpp @@ -486,6 +499,9 @@ namespace CppSharp.Generators.Cpp
var argName = Context.Parameter.Name;
Context.Before.WriteLine($"{type} {argName};");
(uint width, uint _alignment) =
primitive.GetInfo(Context.Context.TargetInfo, out bool _signed);
switch (primitive)
{
case PrimitiveType.Void:
@ -519,31 +535,43 @@ namespace CppSharp.Generators.Cpp @@ -519,31 +535,43 @@ namespace CppSharp.Generators.Cpp
case PrimitiveType.Int:
case PrimitiveType.Long:
if (width == 64)
{
Context.Before.WriteLine($"if (JS_ToBigInt64(ctx, (int64_t*)&{argName}, argv[{Context.ParameterIndex}]))");
Context.Before.WriteLineIndent("return JS_EXCEPTION;");
}
else
{
Context.Before.WriteLine($"if (JS_ToInt32(ctx, &{argName}, argv[{Context.ParameterIndex}]))");
Context.Before.WriteLineIndent("return JS_EXCEPTION;");
}
Context.Return.Write($"{argName}");
return true;
case PrimitiveType.UInt:
case PrimitiveType.ULong:
if (width == 64)
{
Context.Before.WriteLine($"if (JS_ToBigInt64(ctx, (int64_t*)&{argName}, argv[{Context.ParameterIndex}]))");
Context.Before.WriteLineIndent("return JS_EXCEPTION;");
}
else
{
Context.Before.WriteLine($"if (JS_ToUint32(ctx, &{argName}, argv[{Context.ParameterIndex}]))");
Context.Before.WriteLineIndent("return JS_EXCEPTION;");
}
Context.Return.Write($"{argName}");
return true;
case PrimitiveType.LongLong:
Context.Before.WriteLine($"int64_t _{argName};");
Context.Before.WriteLine($"if (JS_ToInt64Ext(ctx, &_{argName}, argv[{Context.ParameterIndex}]))");
Context.Before.WriteLine($"if (JS_ToBigInt64(ctx, (int64_t*)&_{argName}, argv[{Context.ParameterIndex}]))");
Context.Before.WriteLineIndent("return JS_EXCEPTION;");
Context.Before.WriteLine($"{argName} = ({type})_{argName};");
Context.Return.Write($"{argName}");
return true;
case PrimitiveType.ULongLong:
Context.Before.WriteLine($"int64_t _{argName};");
Context.Before.WriteLine($"if (JS_ToInt64Ext(ctx, &_{argName}, argv[{Context.ParameterIndex}]))");
Context.Before.WriteLine($"if (JS_ToBigUInt64(ctx, (int64_t*)&{argName}, argv[{Context.ParameterIndex}]))");
Context.Before.WriteLineIndent("return JS_EXCEPTION;");
Context.Before.WriteLine($"{argName} = ({type})_{argName};");
Context.Return.Write($"{argName}");
return true;

2
src/Generator/Generators/QuickJS/QuickJSSources.cs

@ -912,7 +912,7 @@ namespace CppSharp.Generators.Cpp @@ -912,7 +912,7 @@ namespace CppSharp.Generators.Cpp
public override string GenerateTypeCheckForParameter(int paramIndex, Type type)
{
var typeChecker = new QuickJSTypeCheckGen(paramIndex);
var typeChecker = new QuickJSTypeCheckGen(Context, paramIndex);
type.Visit(typeChecker);
var condition = typeChecker.Generate();

11
src/Generator/Generators/QuickJS/QuickJSTypeCheckGen.cs

@ -11,7 +11,7 @@ namespace CppSharp.Generators.Cpp @@ -11,7 +11,7 @@ namespace CppSharp.Generators.Cpp
public override string FileExtension { get; }
public QuickJSTypeCheckGen(int parameterIndex) : base(null)
public QuickJSTypeCheckGen(BindingContext context, int parameterIndex) : base(context)
{
ParameterIndex = parameterIndex;
}
@ -23,7 +23,8 @@ namespace CppSharp.Generators.Cpp @@ -23,7 +23,8 @@ namespace CppSharp.Generators.Cpp
public override bool VisitPrimitiveType(PrimitiveType primitive, TypeQualifiers quals)
{
// TODO: Use TargetInfo to check the actual width of types for the target.
(uint width, uint _alignment) =
primitive.GetInfo(Context.TargetInfo, out bool _signed);
var condition = string.Empty;
var arg = $"argv[{ParameterIndex}]";
@ -50,10 +51,16 @@ namespace CppSharp.Generators.Cpp @@ -50,10 +51,16 @@ namespace CppSharp.Generators.Cpp
break;
case PrimitiveType.Int:
case PrimitiveType.Long:
if (width == 64)
condition = $"JS_IsBigInt(ctx, {arg})";
else
condition = $"JS_IsInt32({arg})";
break;
case PrimitiveType.ULong:
case PrimitiveType.UInt:
if (width == 64)
condition = $"JS_IsBigInt(ctx, {arg})";
else
condition = $"JS_IsUInt32({arg})";
break;
case PrimitiveType.LongLong:

Loading…
Cancel
Save