Skip to content

Commit

Permalink
feat: quickjs add more func
Browse files Browse the repository at this point in the history
  • Loading branch information
LazuliKao committed Jul 28, 2023
1 parent 046f97f commit fa8a824
Show file tree
Hide file tree
Showing 6 changed files with 446 additions and 90 deletions.
3 changes: 2 additions & 1 deletion src/Loader/Modules/TestModule.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Hosihikari.VanillaScript.QuickJS.Extensions;
using Hosihikari.VanillaScript.QuickJS.Helper;
using Hosihikari.VanillaScript.QuickJS.Types;
using System.Runtime.InteropServices;

Expand All @@ -17,6 +18,6 @@ private static unsafe JsValue test(JsContext* ctx, JsValue val, int argCount, Js
var argv = new ReadOnlySpan<JsValue>(argvIn, argCount);
var arg = argv[0];
Log.Logger.Debug("测试", arg.ToString(ctx));
return JsValueCreateExtension.True;
return JsValueCreateHelper.True;
}
}
9 changes: 7 additions & 2 deletions src/QuickJS/Extensions/JsValueExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ string propertyName
)
{
var val = Native.JS_GetPropertyStr(ctx, @this, propertyName);
return Native.JS_ToCStringLen2(ctx, val.Value);
return Native.JS_ToCString(ctx, val.Value);
}

//JS_ToString
public static unsafe string ToString(this JsValue @this, JsContext* ctx)
{
return Native.JS_ToCStringLen2(ctx, @this);
return Native.JS_ToCString(ctx, @this);
}

public static unsafe bool DefineProperty(
Expand Down Expand Up @@ -110,4 +110,9 @@ public static unsafe bool DefineFunction(
var value = Native.JS_NewCFunction2(ctx, func, funcName, argumentLength, cproto, magic);
return Native.JS_DefinePropertyValueStr(ctx, @this, funcName, value.Value, flags);
}

public static unsafe string ToJson(this JsValue @this, JsContext* ctx)
{
return Native.JS_JSONStringify(ctx, @this);
}
}
10 changes: 10 additions & 0 deletions src/QuickJS/Helper/JsValueCreateHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,14 @@ public static JsValue NewFloat64(double d)
representation */
return u.u == t.u ? MkVal(JsTag.Int, val) : __NewFloat64(d);
}

public static unsafe JsValue NewString(JsContext* ctx, string str)
{
return Native.JS_NewString(ctx, str);
}

public static unsafe JsValue FromJson(JsContext* ctx, string str)
{
return Native.JS_ParseJSON(ctx, str);
}
}
Loading

0 comments on commit fa8a824

Please sign in to comment.