Skip to content

Commit

Permalink
feat: readAllText & writeAllText
Browse files Browse the repository at this point in the history
  • Loading branch information
LazuliKao committed Jul 30, 2023
1 parent fece9fb commit 5cacf68
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 24 deletions.
97 changes: 74 additions & 23 deletions src/Loader/Modules/IO/FileModule.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Hosihikari.VanillaScript.QuickJS.Extensions;
using Hosihikari.VanillaScript.QuickJS;
using Hosihikari.VanillaScript.QuickJS;
using Hosihikari.VanillaScript.QuickJS.Helper;
using Hosihikari.VanillaScript.QuickJS.Types;
using Hosihikari.VanillaScript.QuickJS.Wrapper;
Expand All @@ -20,31 +19,83 @@ public static void Setup(JsContextWrapper ctx)
_ =>
{
using var obj = ctx.NewObject();
#region FileExists
obj.DefineFunction(ctx, "exists", 1, &FileExists);
[UnmanagedCallersOnly]
static JsValue FileExists(
JsContext* ctx,
JsValue thisObj,
int argCount,
JsValue* argvIn
)
{
try
{
var argv = new ReadOnlySpan<JsValue>(argvIn, argCount);
argv.InsureArgumentCount(1);
argv[0].InsureTypeString(ctx, out var file);
return JsValueCreateHelper.NewBool(File.Exists(file));
}
catch (Exception ex)
{
return Native.JS_ThrowInternalError(ctx, ex);
}
}
#endregion
#region FileReadAllText
obj.DefineFunction(ctx, "readAllText", 1, &FileReadAllText);
[UnmanagedCallersOnly]
static JsValue FileReadAllText(
JsContext* ctx,
JsValue thisObj,
int argCount,
JsValue* argvIn
)
{
try
{
var argv = new ReadOnlySpan<JsValue>(argvIn, argCount);
argv.InsureArgumentCount(1);
argv[0].InsureTypeString(ctx, out var file);
return JsValueCreateHelper
.NewString(ctx, File.ReadAllText(file))
.Steal();
}
catch (Exception ex)
{
return Native.JS_ThrowInternalError(ctx, ex);
}
}
#endregion
#region FileWriteAllText
obj.DefineFunction(ctx, "writeAllText", 1, &FileWriteAllText);
[UnmanagedCallersOnly]
static JsValue FileWriteAllText(
JsContext* ctx,
JsValue thisObj,
int argCount,
JsValue* argvIn
)
{
try
{
var argv = new ReadOnlySpan<JsValue>(argvIn, argCount);
argv.InsureArgumentCount(2);
argv[0].InsureTypeString(ctx, out var file);
argv[1].InsureTypeString(ctx, out var content);
File.WriteAllText(file, content);
return JsValueCreateHelper.NewBool(true);
}
catch (Exception ex)
{
return Native.JS_ThrowInternalError(ctx, ex);
}
}
#endregion
return obj.Steal();
}
);
}
}

[UnmanagedCallersOnly]
private static unsafe JsValue FileExists(
JsContext* ctx,
JsValue val,
int argCount,
JsValue* argvIn
)
{
try
{
var argv = new ReadOnlySpan<JsValue>(argvIn, argCount);
argv.InsureArgumentCount(1);
argv[0].InsureTypeString(ctx, out var file);
return JsValueCreateHelper.NewBool(File.Exists(file));
}
catch (Exception ex)
{
return Native.JS_ThrowInternalError(ctx, ex.Message);
}
}
}
2 changes: 1 addition & 1 deletion src/QuickJS/Wrapper/JsContextWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public AutoDropJsValue NewJsFunction(
}
catch (Exception ex)
{ //pass the exception to js context
Native.JS_ThrowInternalError(ctx, ex.ToString());
Native.JS_ThrowInternalError(ctx, ex);
return JsValueCreateHelper.Exception;
}
}
Expand Down

0 comments on commit 5cacf68

Please sign in to comment.