Browse Source

Changes

quickjs
Joao Matos 2 years ago
parent
commit
6c057977d0
  1. 118
      src/Generator/Generators/QuickJS/QuickJSMarshal.cs
  2. 2
      tests/quickjs/premake5.lua
  3. 2
      tests/quickjs/test.sh

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

@ -61,34 +61,22 @@ namespace CppSharp.Generators.Cpp
var pointee = pointer.Pointee.Desugar(); var pointee = pointer.Pointee.Desugar();
PrimitiveType primitive;
var param = Context.Parameter; var param = Context.Parameter;
if (param != null && (param.IsOut || param.IsInOut) && if (param != null && (param.IsOut || param.IsInOut) &&
pointee.IsPrimitiveType(out primitive)) pointee.IsPrimitiveType(out _))
{ {
Context.Return.Write(Context.ReturnVarName); Context.Return.Write(Context.ReturnVarName);
return true; return true;
} }
if (pointee.IsPrimitiveType(out primitive)) if (pointee.IsPrimitiveType(out _))
{ {
var returnVarName = Context.ReturnVarName; if (pointer.IsConstCharString())
if (pointer.GetFinalQualifiedPointee().Qualifiers.IsConst !=
Context.ReturnType.Qualifiers.IsConst)
{ {
var nativeTypePrinter = new CppTypePrinter(Context.Context) var retName = Generator.GeneratedIdentifier(Context.ReturnVarName);
{ PrintTypeQualifiers = false }; Context.Before.Write($"JSValue {retName} = JS_NewString(ctx, {Context.ArgName});");
var returnType = Context.ReturnType.Type.Desugar(); Context.Return.Write(retName);
var constlessPointer = new PointerType() return true;
{
IsDependent = pointer.IsDependent,
Modifier = pointer.Modifier,
QualifiedPointee = new QualifiedType(returnType.GetPointee())
};
var nativeConstlessTypeName = constlessPointer.Visit(nativeTypePrinter, new TypeQualifiers());
returnVarName = string.Format("const_cast<{0}>({1})",
nativeConstlessTypeName, Context.ReturnVarName);
} }
if (pointer.Pointee is TypedefType) if (pointer.Pointee is TypedefType)
@ -101,19 +89,17 @@ namespace CppSharp.Generators.Cpp
}; };
var nativeTypeName = desugaredPointer.Visit(typePrinter, quals); var nativeTypeName = desugaredPointer.Visit(typePrinter, quals);
Context.Return.Write("reinterpret_cast<{0}>({1})", nativeTypeName, Context.Return.Write("reinterpret_cast<{0}>({1})", nativeTypeName,
returnVarName); Context.ReturnVarName);
} }
else else
Context.Return.Write(returnVarName); Context.Return.Write(Context.ReturnVarName);
return true; return true;
} }
TypeMap typeMap = null; Context.Context.TypeMaps.FindTypeMap(pointee, out var typeMap);
Context.Context.TypeMaps.FindTypeMap(pointee, out typeMap);
Class @class; if (pointee.TryGetClass(out var @class) && typeMap == null)
if (pointee.TryGetClass(out @class) && typeMap == null)
{ {
var instance = (pointer.IsReference) ? "&" + Context.ReturnVarName var instance = (pointer.IsReference) ? "&" + Context.ReturnVarName
: Context.ReturnVarName; : Context.ReturnVarName;
@ -149,7 +135,8 @@ namespace CppSharp.Generators.Cpp
switch (primitive) switch (primitive)
{ {
case PrimitiveType.Void: case PrimitiveType.Void:
return true; Context.Before.WriteLine($"JS_UNDEFINED;");
break;
case PrimitiveType.Bool: case PrimitiveType.Bool:
Context.Before.WriteLine($"JS_NewBool(ctx, {Context.ArgName});"); Context.Before.WriteLine($"JS_NewBool(ctx, {Context.ArgName});");
@ -205,8 +192,7 @@ namespace CppSharp.Generators.Cpp
{ {
var decl = typedef.Declaration; var decl = typedef.Declaration;
TypeMap typeMap; if (Context.Context.TypeMaps.FindTypeMap(decl.Type, out var typeMap) &&
if (Context.Context.TypeMaps.FindTypeMap(decl.Type, out typeMap) &&
typeMap.DoesMarshalling) typeMap.DoesMarshalling)
{ {
typeMap.Type = typedef; typeMap.Type = typedef;
@ -214,8 +200,7 @@ namespace CppSharp.Generators.Cpp
return typeMap.IsValueType; return typeMap.IsValueType;
} }
FunctionType function; if (decl.Type.IsPointerTo(out FunctionType _))
if (decl.Type.IsPointerTo(out function))
{ {
var typeName = typePrinter.VisitDeclaration(decl); var typeName = typePrinter.VisitDeclaration(decl);
var typeName2 = decl.Type.Visit(typePrinter); var typeName2 = decl.Type.Visit(typePrinter);
@ -228,8 +213,7 @@ namespace CppSharp.Generators.Cpp
public override bool VisitTemplateSpecializationType(TemplateSpecializationType template, public override bool VisitTemplateSpecializationType(TemplateSpecializationType template,
TypeQualifiers quals) TypeQualifiers quals)
{ {
TypeMap typeMap; if (Context.Context.TypeMaps.FindTypeMap(template, out var typeMap) && typeMap.DoesMarshalling)
if (Context.Context.TypeMaps.FindTypeMap(template, out typeMap) && typeMap.DoesMarshalling)
{ {
typeMap.Type = template; typeMap.Type = template;
typeMap.MarshalToManaged(GeneratorKind.QuickJS, Context); typeMap.MarshalToManaged(GeneratorKind.QuickJS, Context);
@ -382,8 +366,7 @@ namespace CppSharp.Generators.Cpp
public override bool VisitType(Type type, TypeQualifiers quals) public override bool VisitType(Type type, TypeQualifiers quals)
{ {
TypeMap typeMap; if (Context.Context.TypeMaps.FindTypeMap(type, out var typeMap) && typeMap.DoesMarshalling)
if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling)
{ {
typeMap.MarshalToNative(GeneratorKind.QuickJS, Context); typeMap.MarshalToNative(GeneratorKind.QuickJS, Context);
return false; return false;
@ -443,8 +426,7 @@ namespace CppSharp.Generators.Cpp
return VisitDelegateType(cppTypeName); return VisitDelegateType(cppTypeName);
} }
Enumeration @enum; if (pointee.TryGetEnum(out var @enum))
if (pointee.TryGetEnum(out @enum))
{ {
var isRef = Context.Parameter.Usage == ParameterUsage.Out || var isRef = Context.Parameter.Usage == ParameterUsage.Out ||
Context.Parameter.Usage == ParameterUsage.InOut; Context.Parameter.Usage == ParameterUsage.InOut;
@ -455,14 +437,24 @@ namespace CppSharp.Generators.Cpp
return true; return true;
} }
Class @class; if (pointee.TryGetClass(out var @class) && @class.IsValueType)
if (pointee.TryGetClass(out @class) && @class.IsValueType)
{ {
if (Context.Function == null) if (Context.Function == null)
Context.Return.Write("&"); Context.Return.Write("&");
return pointer.QualifiedPointee.Visit(this); return pointer.QualifiedPointee.Visit(this);
} }
if (pointer.IsConstCharString())
{
var genName = Generator.GeneratedIdentifier(Context.Parameter.Name);
Context.Before.WriteLine($"auto {genName} = JS_ToCString(ctx, argv[{Context.ParameterIndex}]);");
Context.Before.WriteLine($"if ({genName} == NULL)");
Context.Before.WriteLineIndent("return JS_EXCEPTION;");
Context.Return.Write($"{genName}");
Context.Cleanup.WriteLine($"JS_FreeCString(ctx, {genName});");
return true;
}
var finalPointee = pointer.GetFinalPointee(); var finalPointee = pointer.GetFinalPointee();
if (finalPointee.IsPrimitiveType()) if (finalPointee.IsPrimitiveType())
{ {
@ -569,6 +561,12 @@ namespace CppSharp.Generators.Cpp
Context.Return.Write($"{argName}"); Context.Return.Write($"{argName}");
return true; return true;
case PrimitiveType.Null:
Context.Before.WriteLine($"if (!JS_IsNull(argv[{Context.ParameterIndex}]))");
Context.Before.WriteLineIndent("return JS_EXCEPTION;");
Context.Return.Write($"{argName}");
return true;
case PrimitiveType.WideChar: case PrimitiveType.WideChar:
default: default:
throw new NotImplementedException(); throw new NotImplementedException();
@ -579,16 +577,14 @@ namespace CppSharp.Generators.Cpp
{ {
var decl = typedef.Declaration; var decl = typedef.Declaration;
TypeMap typeMap; if (Context.Context.TypeMaps.FindTypeMap(decl.Type, out var typeMap) &&
if (Context.Context.TypeMaps.FindTypeMap(decl.Type, out typeMap) &&
typeMap.DoesMarshalling) typeMap.DoesMarshalling)
{ {
typeMap.MarshalToNative(GeneratorKind.QuickJS, Context); typeMap.MarshalToNative(GeneratorKind.QuickJS, Context);
return typeMap.IsValueType; return typeMap.IsValueType;
} }
FunctionType func; if (decl.Type.IsPointerTo(out FunctionType _))
if (decl.Type.IsPointerTo(out func))
{ {
typePrinter.PushContext(TypePrinterContextKind.Native); typePrinter.PushContext(TypePrinterContextKind.Native);
var declName = decl.Visit(typePrinter); var declName = decl.Visit(typePrinter);
@ -609,8 +605,7 @@ namespace CppSharp.Generators.Cpp
return true; return true;
} }
PrimitiveType primitive; if (decl.Type.IsPrimitiveType(out _))
if (decl.Type.IsPrimitiveType(out primitive))
{ {
Context.Return.Write($"(::{typedef.Declaration.QualifiedOriginalName})"); Context.Return.Write($"(::{typedef.Declaration.QualifiedOriginalName})");
} }
@ -621,8 +616,7 @@ namespace CppSharp.Generators.Cpp
public override bool VisitTemplateSpecializationType(TemplateSpecializationType template, public override bool VisitTemplateSpecializationType(TemplateSpecializationType template,
TypeQualifiers quals) TypeQualifiers quals)
{ {
TypeMap typeMap; if (Context.Context.TypeMaps.FindTypeMap(template, out var typeMap) && typeMap.DoesMarshalling)
if (Context.Context.TypeMaps.FindTypeMap(template, out typeMap) && typeMap.DoesMarshalling)
{ {
typeMap.Type = template; typeMap.Type = template;
typeMap.MarshalToNative(GeneratorKind.QuickJS, Context); typeMap.MarshalToNative(GeneratorKind.QuickJS, Context);
@ -668,42 +662,14 @@ namespace CppSharp.Generators.Cpp
private void MarshalRefClass(Class @class) private void MarshalRefClass(Class @class)
{ {
var type = Context.Parameter.Type.Desugar(); var type = Context.Parameter.Type.Desugar();
TypeMap typeMap; if (Context.Context.TypeMaps.FindTypeMap(type, out var typeMap) &&
if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) &&
typeMap.DoesMarshalling) typeMap.DoesMarshalling)
{ {
typeMap.MarshalToNative(GeneratorKind.QuickJS, Context); typeMap.MarshalToNative(GeneratorKind.QuickJS, Context);
return; return;
} }
if (!type.SkipPointerRefs().IsPointer()) Context.Return.Write($"({@class.QualifiedOriginalName}*) JS_GetOpaque(argv[{Context.ParameterIndex}], 0)");
{
Context.Return.Write("*");
if (Context.Parameter.Type.IsReference())
VarPrefix.Write("&");
}
var method = Context.Function as Method;
if (method != null
&& method.Conversion == MethodConversionKind.FunctionToInstanceMethod
&& Context.ParameterIndex == 0)
{
Context.Return.Write($"(::{@class.QualifiedOriginalName}*)");
Context.Return.Write(Helpers.InstanceIdentifier);
return;
}
var paramType = Context.Parameter.Type.Desugar();
var isPointer = paramType.SkipPointerRefs().IsPointer();
var deref = isPointer ? "->" : ".";
var instance = $"(::{@class.QualifiedOriginalName}*)" +
$"{Context.Parameter.Name}{deref}{Helpers.InstanceIdentifier}";
if (isPointer)
Context.Return.Write($"{Context.Parameter.Name} ? {instance} : nullptr");
else
Context.Return.Write($"{instance}");
} }
private void MarshalValueClass(Class @class) private void MarshalValueClass(Class @class)

2
tests/quickjs/premake5.lua

@ -1,4 +1,4 @@
local qjs_dir = path.getabsolute("../../deps/quickjs") local qjs_dir = path.getabsolute("./quickjs")
local runtime = "../../src/Generator/Generators/QuickJS/Runtime" local runtime = "../../src/Generator/Generators/QuickJS/Runtime"
workspace "qjs" workspace "qjs"

2
tests/quickjs/test.sh

@ -5,7 +5,7 @@ rootdir="$dir/../.."
dotnet_configuration=Release dotnet_configuration=Release
configuration=debug configuration=debug
platform=x64 platform=x64
jsinterp="$rootdir/deps/quickjs/qjs-debug" jsinterp="$dir/quickjs/qjs"
red=`tput setaf 1` red=`tput setaf 1`
green=`tput setaf 2` green=`tput setaf 2`

Loading…
Cancel
Save