Skip to content

Commit

Permalink
feat: add JsClassDef & JsPropertyDescriptor struct
Browse files Browse the repository at this point in the history
  • Loading branch information
LazuliKao committed Jul 31, 2023
1 parent b565230 commit 76823b0
Show file tree
Hide file tree
Showing 3 changed files with 208 additions and 6 deletions.
69 changes: 63 additions & 6 deletions src/QuickJS/Native.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Hosihikari.Logging;
using Hosihikari.NativeInterop;

Check failure on line 3 in src/QuickJS/Native.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'NativeInterop' does not exist in the namespace 'Hosihikari' (are you missing an assembly reference?)
using Hosihikari.NativeInterop.Utils;
using Hosihikari.VanillaScript.QuickJS.Exceptions;
Expand All @@ -10,6 +9,7 @@
using Hosihikari.VanillaScript.QuickJS.Wrapper;
using size_t = nuint;
using JsAtom = System.UInt32;
using JSClassID = System.UInt32;

namespace Hosihikari.VanillaScript.QuickJS;

Expand Down Expand Up @@ -686,7 +686,6 @@ public static string JS_GetScriptOrModuleName(JsContext* ctx, int nStackLevels)
);
#endregion
#region JS_AtomToCString

//const char *JS_AtomToCString(JSContext *ctx, JSAtom atom)
public static string JS_AtomToCString(JsContext* ctx, JsAtom atom)
{
Expand All @@ -704,8 +703,7 @@ public static string JS_AtomToCString(JsContext* ctx, JsAtom atom)
//void JS_FreeAtom(JSContext *ctx, JSAtom v)
public static void JS_FreeAtom(JsContext* ctx, JsAtom v)
{
var func = (delegate* unmanaged<JsContext*, JsAtom, void>)_ptrJsFreeAtom.Value;
func(ctx, v);
((delegate* unmanaged<JsContext*, JsAtom, void>)_ptrJsFreeAtom.Value)(ctx, v);
}

private static readonly Lazy<nint> _ptrJsFreeAtom = GetPointerLazy("JS_FreeAtom");
Expand Down Expand Up @@ -806,8 +804,6 @@ public static bool JS_SetPropertyUint32(JsContext* ctx, JsValue thisObj, uint id
"JS_SetPropertyUint32"
);
#endregion


#region JS_GetOwnPropertyNames
/*int JS_GetOwnPropertyNames(JSContext *ctx, JSPropertyEnum **ptab,
uint32_t *plen, JSValueConst obj, int flags)*/
Expand Down Expand Up @@ -854,5 +850,66 @@ public static void js_free(JsContext* ctx, void* ptr)
}

private static readonly Lazy<nint> _ptrJsFree = GetPointerLazy("js_free");
#endregion
#region JS_NewClassID
//JSClassID JS_NewClassID(JSClassID *pclass_id);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static JSClassID JS_NewClassID()
{
uint classId = 0;
((delegate* unmanaged<JSClassID*, JSClassID>)_ptrJsNewClassId.Value)(&classId);
return classId;
}

private static readonly Lazy<nint> _ptrJsNewClassId = GetPointerLazy("JS_NewClassID");
#endregion
#region JS_IsRegisteredClass

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool JS_IsRegisteredClass(JsRuntime* ctx, JSClassID classId)
{
return ((delegate* unmanaged<JsRuntime*, JSClassID, int>)_ptrJsIsRegisteredClass.Value)(
ctx,
classId
) != 0;
}

private static readonly Lazy<nint> _ptrJsIsRegisteredClass = GetPointerLazy(
"JS_IsRegisteredClass"
);
#endregion
#region JS_GetRuntime

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static JsRuntime* JS_GetRuntime(JsContext* ctx)
{
//or ctx->rt;
return ((delegate* unmanaged<JsContext*, JsRuntime*>)_ptrJsGetRuntime.Value)(ctx);
}

private static readonly Lazy<nint> _ptrJsGetRuntime = GetPointerLazy("JS_GetRuntime");
#endregion
#region JS_NewClass

//int JS_NewClass(JSRuntime *rt, JSClassID class_id, const JSClassDef *class_def)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int JS_NewClass(JsRuntime* rt, JSClassID classId, JsClassDef* classDef)
{
return ((delegate* unmanaged<JsRuntime*, JSClassID, JsClassDef*, int>)_ptrJsNewClass.Value)(
rt,
classId,
classDef
);
}

private static readonly Lazy<nint> _ptrJsNewClass = GetPointerLazy("JS_NewClass");

#endregion
#region JS_NewObjectProtoClass
//JSValue JS_NewObjectProtoClass(JSContext *ctx, JSValueConst proto, JSClassID class_id)




#endregion
}
128 changes: 128 additions & 0 deletions src/QuickJS/Types/JsClassDef.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
namespace Hosihikari.VanillaScript.QuickJS.Types;

using JsAtom = UInt32;

//typedef struct JSClassDef
public unsafe ref struct JsClassDef
{
// const char* class_name;
public byte* ClassName;

// JSClassFinalizer* finalizer;
//typedef void JSClassFinalizer(JSRuntime *rt, JSValue val);
public delegate* unmanaged<JsRuntime*, JsValue, void> Finalizer;

// JSClassGCMark* gc_mark;
//typedef void JSClassGCMark(JSRuntime *rt, JSValueConst val,JS_MarkFunc* mark_func);
//typedef void JS_MarkFunc(JSRuntime* rt, JSGCObjectHeader* gp);
public delegate* unmanaged<
JsRuntime*,
JsValue,
delegate* unmanaged<JsRuntime*, JsGCObjectHeader*, void>,
void> GcMark;

// /* */
// JSClassCall* call;
//typedef JSValue JSClassCall(JSContext *ctx, JSValueConst func_obj,JSValueConst this_val, int argc, JSValueConst *argv,int flags);
/// <summary>
/// if call != NULL, the object is a function. If (flags &
/// JS_CALL_FLAG_CONSTRUCTOR) != 0, the function is called as a
/// constructor. In this case, 'this_val' is new.target. A
/// constructor call only happens if the object constructor bit is
/// set (see JS_SetConstructorBit()).
/// </summary>
public delegate* unmanaged<JsContext*, JsValue, JsValue, int, JsValue*, int, JsValue> Call;

public JsClassExoticMethods* Exotic;

#region JsClassExoticMethods
//typedef struct JSClassExoticMethods
public ref struct JsClassExoticMethods
{
// int (* get_own_property) (JSContext* ctx, JSPropertyDescriptor* desc,
// JSValueConst obj, JSAtom prop);
/// <summary>
///Return -1 if exception (can only happen in case of Proxy object),
/// FALSE if the property does not exists, TRUE if it exists. If 1 is
/// returned, the property descriptor 'desc' is filled if != NULL.
/// </summary>
public delegate* unmanaged<
JsContext*,
JsPropertyDescriptor*,
JsValue,
JsAtom,
int> GetOwnProperty;

// int (* get_own_property_names) (JSContext* ctx, JSPropertyEnum** ptab,
// uint32_t* plen,
// JSValueConst obj);
/// <summary>
/// '*ptab' should hold the '*plen' property keys. Return 0 if OK,
/// -1 if exception. The 'is_enumerable' field is ignored.
/// </summary>
public delegate* unmanaged<
JsContext*,
JsPropertyEnum**,
uint*,
JsValue,
int> GetOwnPropertyNames;

// int (* delete_property) (JSContext* ctx, JSValueConst obj, JSAtom prop);
/// <summary>
/// return &lt; 0 if exception, or TRUE/FALSE
/// </summary>
public delegate* unmanaged<JsContext*, JsValue, JsAtom, int> DeleteProperty;

// int (* define_own_property) (JSContext* ctx, JSValueConst this_obj,
// JSAtom prop, JSValueConst val,
// JSValueConst getter, JSValueConst setter,
// int flags);
/// <summary>
/// return &lt; 0 if exception or TRUE/FALSE
/// </summary>
public delegate* unmanaged<
JsContext*,
JsValue, //this_obj
JsAtom, //prop
JsValue, //val
JsValue, //getter
JsValue, //setter
JsPropertyFlags, //flags
int> DefineOwnProperty;

/* The following methods can be emulated with the previous ones,
so they are usually not needed */

// int (* has_property) (JSContext* ctx, JSValueConst obj, JSAtom atom);
/// <summary>
/// The following methods can be emulated with the previous ones,so they are usually not needed
/// return &lt; 0 if exception or TRUE/FALSE
/// </summary>
public delegate* unmanaged<JsContext*, JsValue, JsAtom, int> HasProperty;

// JSValue(*get_property)(JSContext* ctx, JSValueConst obj, JSAtom atom,
// JSValueConst receiver);
/// <summary>
/// The following methods can be emulated with the previous ones,so they are usually not needed
/// </summary>
public delegate* unmanaged<JsContext*, JsValue, JsAtom, JsValue, JsValue> GetProperty;

// int (* set_property) (JSContext* ctx, JSValueConst obj, JSAtom atom,
// JSValueConst value, JSValueConst receiver, int flags);
/// <summary>
/// return &lt; 0 if exception or TRUE/FALSE
/// The following methods can be emulated with the previous ones,so they are usually not needed
/// </summary>
public delegate* unmanaged<
JsContext*,
JsValue, //obj
JsAtom, //atom
JsValue, //value
JsValue, //receiver
JsPropertyFlags, //flags
int> SetProperty;
}
//JSClassExoticMethods;
#endregion
}
//JSClassDef;
17 changes: 17 additions & 0 deletions src/QuickJS/Types/JsPropertyDescriptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Hosihikari.VanillaScript.QuickJS.Types;

public ref struct JsPropertyDescriptor
{
/*
typedef struct JSPropertyDescriptor {
int flags;
JSValue value;
JSValue getter;
JSValue setter;
} JSPropertyDescriptor;
*/
public int Flags;
public JsValue Value;
public JsValue Getter;
public JsValue Setter;
}

0 comments on commit 76823b0

Please sign in to comment.