Skip to content

Commit

Permalink
Implement JSContextGroup
Browse files Browse the repository at this point in the history
Implement JSContext and JSGlobalContext
Move implementation of JSNativeContainer from JSString.cs to JSBase.cs
Make constructor of JSString private
  • Loading branch information
SupinePandora43 committed Aug 22, 2023
1 parent 3bc8bb7 commit a79cc78
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 56 deletions.
13 changes: 13 additions & 0 deletions src/UltralightNet/JavaScript/JSBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using UltralightNet.JavaScript.Low;
using UltralightNet.LowStuff;

namespace UltralightNet.JavaScript
{
Expand Down Expand Up @@ -42,4 +44,15 @@ public static bool JSCheckScriptSyntax(JSContextRef context, JSStringRef script,
public static partial void JSGarbageCollect(JSContextRef context);
}
}
namespace LowStuff
{
public abstract unsafe class JSNativeContainer<NativeHandle> : NativeContainer where NativeHandle : unmanaged
{
public NativeHandle JSHandle
{
get => JavaScriptMethods.BitCast<nuint, NativeHandle>((nuint)Handle);
protected init => Handle = (void*)JavaScriptMethods.BitCast<NativeHandle, nuint>(value);
}
}
}
}
101 changes: 92 additions & 9 deletions src/UltralightNet/JavaScript/JSContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// JSContextRef.h

using System.Runtime.InteropServices;
using UltralightNet.JavaScript.Low;
using UltralightNet.JavaScript.LowStuff;

namespace UltralightNet.JavaScript
{
Expand Down Expand Up @@ -66,6 +68,7 @@ public readonly struct JSGlobalContextRef
public static bool operator !=(JSGlobalContextRef left, JSGlobalContextRef right) => left._handle != right._handle;

public static implicit operator JSContextRef(JSGlobalContextRef globalContextRef) => JavaScriptMethods.BitCast<JSGlobalContextRef, JSContextRef>(globalContextRef);
public static explicit operator JSGlobalContextRef(JSContextRef contextRef) => JavaScriptMethods.BitCast<JSContextRef, JSGlobalContextRef>(contextRef);
}
public readonly struct JSContextRef
{
Expand All @@ -78,18 +81,41 @@ public readonly struct JSContextRef
public static bool operator !=(JSContextRef left, JSContextRef right) => left._handle != right._handle;
}
}
/*public unsafe class JSContext : IDisposable

public unsafe sealed class JSContextGroup : JSNativeContainer<JSContextGroupRef>, ICloneable
{
public JSContext() { }
private JSContextGroup() { }

public JSContextGroup Clone()
{
JSContextGroup returnValue = FromHandle(JavaScriptMethods.JSContextGroupRetain(JSHandle), true);
GC.KeepAlive(this);
return returnValue;
}
object ICloneable.Clone() => Clone();

internal void* handle;
internal bool isGlobalContext = false;
private bool isDisposed = false;
private bool dispose = true;
public JSGlobalContext CreateGlobalContext(JSNativeContainer<JSClassRef>? globalObject = null)
{
var handle = JavaScriptMethods.JSGlobalContextCreateInGroup(JSHandle, globalObject?.JSHandle ?? default);
GC.KeepAlive(this);
GC.KeepAlive(globalObject);
return JSGlobalContext.FromHandle(handle, true);
}

public static JSContextGroup FromHandle(JSContextGroupRef handle, bool dispose) => new() { JSHandle = handle, Owns = dispose };

public override void Dispose()
{
if (!IsDisposed && Owns) JavaScriptMethods.JSContextGroupRelease(JSHandle);
base.Dispose();
}
}

public void* Handle => handle;
public unsafe class JSContext : JSNativeContainer<JSContextRef>
{
internal protected JSContext() { }

internal void OnLocked(void* actualHandle)
/*internal void OnLocked(void* actualHandle)
{
handle = actualHandle;
}
Expand Down Expand Up @@ -160,6 +186,63 @@ public void Dispose()
isDisposed = true;
GC.SuppressFinalize(this);
}
}*/

// TODO: GlobalObject
public JSContextGroup Group
{
get
{
var returnValue = JSContextGroup.FromHandle(JavaScriptMethods.JSContextGroupRetain(JavaScriptMethods.JSContextGetGroup(JSHandle)), true);
GC.KeepAlive(this);
return returnValue;
}
}
public JSGlobalContext GlobalContext
{
get
{
throw new NotSupportedException("Returned instance of view's locked context is invalid.");
var returnValue = JSGlobalContext.FromHandle(JavaScriptMethods.JSGlobalContextRetain(JavaScriptMethods.JSContextGetGlobalContext(JSHandle)), true);

Check warning on line 206 in src/UltralightNet/JavaScript/JSContext.cs

View workflow job for this annotation

GitHub Actions / Build

Unreachable code detected

Check warning on line 206 in src/UltralightNet/JavaScript/JSContext.cs

View workflow job for this annotation

GitHub Actions / Build

Unreachable code detected

Check warning on line 206 in src/UltralightNet/JavaScript/JSContext.cs

View workflow job for this annotation

GitHub Actions / Build

Unreachable code detected

Check warning on line 206 in src/UltralightNet/JavaScript/JSContext.cs

View workflow job for this annotation

GitHub Actions / Test (windows)

Unreachable code detected

Check warning on line 206 in src/UltralightNet/JavaScript/JSContext.cs

View workflow job for this annotation

GitHub Actions / Test (windows)

Unreachable code detected

Check warning on line 206 in src/UltralightNet/JavaScript/JSContext.cs

View workflow job for this annotation

GitHub Actions / Test (windows)

Unreachable code detected

Check warning on line 206 in src/UltralightNet/JavaScript/JSContext.cs

View workflow job for this annotation

GitHub Actions / Test (windows)

Unreachable code detected

Check warning on line 206 in src/UltralightNet/JavaScript/JSContext.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu)

Unreachable code detected

Check warning on line 206 in src/UltralightNet/JavaScript/JSContext.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu)

Unreachable code detected

Check warning on line 206 in src/UltralightNet/JavaScript/JSContext.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu)

Unreachable code detected

Check warning on line 206 in src/UltralightNet/JavaScript/JSContext.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu)

Unreachable code detected
GC.KeepAlive(this);
return returnValue;
}
}
}*/

public static JSContext FromHandle(JSContextRef handle, bool dispose) => new() { JSHandle = handle, Owns = dispose };
}
public unsafe sealed class JSGlobalContext : JSContext
{
private JSGlobalContext() { }

public new JSGlobalContextRef JSHandle
{
get => (JSGlobalContextRef)base.JSHandle;
private init => base.JSHandle = value;
}

public JSString Name
{
get
{
var returnValue = JSString.FromHandle(JavaScriptMethods.JSGlobalContextCopyName(JSHandle), true);
GC.KeepAlive(this);
return returnValue;
}
set
{
JavaScriptMethods.JSGlobalContextSetName(JSHandle, value.JSHandle);
GC.KeepAlive(this);
GC.KeepAlive(value);
}
}

public static JSGlobalContext FromHandle(JSGlobalContextRef handle, bool dispose) => new() { JSHandle = handle, Owns = dispose };

public override void Dispose()
{
if (!IsDisposed && Owns) JavaScriptMethods.JSGlobalContextRelease(JSHandle);
base.Dispose();
}
}
}
36 changes: 0 additions & 36 deletions src/UltralightNet/JavaScript/JSContextGroup.cs

This file was deleted.

13 changes: 2 additions & 11 deletions src/UltralightNet/JavaScript/JSString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,12 @@ public readonly struct JSStringRef
public static bool operator !=(JSStringRef left, JSStringRef right) => left._handle != right._handle;
}
}
namespace LowStuff
{
public abstract unsafe class JSNativeContainer<NativeHandle> : NativeContainer where NativeHandle : unmanaged
{
public NativeHandle JSHandle
{
get => JavaScriptMethods.BitCast<nuint, NativeHandle>((nuint)Handle);
protected init => Handle = (void*)JavaScriptMethods.BitCast<NativeHandle, nuint>(value);
}
}
}

[DebuggerDisplay("{ToString(),raw}")]
public unsafe sealed class JSString : JSNativeContainer<JSStringRef>, IEquatable<JSString>, IEquatable<string>, ICloneable

Check warning on line 64 in src/UltralightNet/JavaScript/JSString.cs

View workflow job for this annotation

GitHub Actions / Build

'JSString' defines operator == or operator != but does not override Object.Equals(object o)

Check warning on line 64 in src/UltralightNet/JavaScript/JSString.cs

View workflow job for this annotation

GitHub Actions / Build

'JSString' defines operator == or operator != but does not override Object.GetHashCode()

Check warning on line 64 in src/UltralightNet/JavaScript/JSString.cs

View workflow job for this annotation

GitHub Actions / Build

'JSString' defines operator == or operator != but does not override Object.Equals(object o)

Check warning on line 64 in src/UltralightNet/JavaScript/JSString.cs

View workflow job for this annotation

GitHub Actions / Build

'JSString' defines operator == or operator != but does not override Object.GetHashCode()

Check warning on line 64 in src/UltralightNet/JavaScript/JSString.cs

View workflow job for this annotation

GitHub Actions / Build

'JSString' defines operator == or operator != but does not override Object.Equals(object o)

Check warning on line 64 in src/UltralightNet/JavaScript/JSString.cs

View workflow job for this annotation

GitHub Actions / Build

'JSString' defines operator == or operator != but does not override Object.GetHashCode()

Check warning on line 64 in src/UltralightNet/JavaScript/JSString.cs

View workflow job for this annotation

GitHub Actions / Test (windows)

'JSString' defines operator == or operator != but does not override Object.Equals(object o)

Check warning on line 64 in src/UltralightNet/JavaScript/JSString.cs

View workflow job for this annotation

GitHub Actions / Test (windows)

'JSString' defines operator == or operator != but does not override Object.GetHashCode()

Check warning on line 64 in src/UltralightNet/JavaScript/JSString.cs

View workflow job for this annotation

GitHub Actions / Test (windows)

'JSString' defines operator == or operator != but does not override Object.Equals(object o)

Check warning on line 64 in src/UltralightNet/JavaScript/JSString.cs

View workflow job for this annotation

GitHub Actions / Test (windows)

'JSString' defines operator == or operator != but does not override Object.GetHashCode()

Check warning on line 64 in src/UltralightNet/JavaScript/JSString.cs

View workflow job for this annotation

GitHub Actions / Test (windows)

'JSString' defines operator == or operator != but does not override Object.Equals(object o)

Check warning on line 64 in src/UltralightNet/JavaScript/JSString.cs

View workflow job for this annotation

GitHub Actions / Test (windows)

'JSString' defines operator == or operator != but does not override Object.GetHashCode()

Check warning on line 64 in src/UltralightNet/JavaScript/JSString.cs

View workflow job for this annotation

GitHub Actions / Test (windows)

'JSString' defines operator == or operator != but does not override Object.Equals(object o)

Check warning on line 64 in src/UltralightNet/JavaScript/JSString.cs

View workflow job for this annotation

GitHub Actions / Test (windows)

'JSString' defines operator == or operator != but does not override Object.GetHashCode()

Check warning on line 64 in src/UltralightNet/JavaScript/JSString.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu)

'JSString' defines operator == or operator != but does not override Object.Equals(object o)

Check warning on line 64 in src/UltralightNet/JavaScript/JSString.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu)

'JSString' defines operator == or operator != but does not override Object.GetHashCode()

Check warning on line 64 in src/UltralightNet/JavaScript/JSString.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu)

'JSString' defines operator == or operator != but does not override Object.Equals(object o)

Check warning on line 64 in src/UltralightNet/JavaScript/JSString.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu)

'JSString' defines operator == or operator != but does not override Object.GetHashCode()

Check warning on line 64 in src/UltralightNet/JavaScript/JSString.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu)

'JSString' defines operator == or operator != but does not override Object.Equals(object o)

Check warning on line 64 in src/UltralightNet/JavaScript/JSString.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu)

'JSString' defines operator == or operator != but does not override Object.GetHashCode()

Check warning on line 64 in src/UltralightNet/JavaScript/JSString.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu)

'JSString' defines operator == or operator != but does not override Object.Equals(object o)

Check warning on line 64 in src/UltralightNet/JavaScript/JSString.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu)

'JSString' defines operator == or operator != but does not override Object.GetHashCode()
{
private JSString() { }

public JSString Clone()
{
JSString returnValue = FromHandle(JavaScriptMethods.JSStringRetain(JSHandle), true);
Expand Down

0 comments on commit a79cc78

Please sign in to comment.