-
-
Notifications
You must be signed in to change notification settings - Fork 560
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Read DataView and ArrayBuffer as byte array from engine (#1786)
- Loading branch information
Showing
5 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System.Text; | ||
|
||
namespace Jint.Tests.Runtime.Domain; | ||
|
||
/// <summary> | ||
/// https://encoding.spec.whatwg.org/#textdecoder | ||
/// </summary> | ||
/// <remarks>Public API, do not make internal!</remarks> | ||
public sealed class TextDecoder | ||
{ | ||
public string Decode() => string.Empty; | ||
|
||
|
||
public string Decode(byte[] buff) => Encoding.UTF8.GetString(buff); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using Jint.Runtime.Interop; | ||
using Jint.Tests.Runtime.Domain; | ||
|
||
namespace Jint.Tests.Runtime; | ||
|
||
public sealed class TextTests | ||
{ | ||
private readonly Engine _engine; | ||
|
||
public TextTests() | ||
{ | ||
_engine = new Engine() | ||
.SetValue("log", new Action<object>(Console.WriteLine)) | ||
.SetValue("assert", new Action<bool>(Assert.True)) | ||
.SetValue("equal", new Action<object, object>(Assert.Equal)); | ||
_engine | ||
.SetValue("TextDecoder", TypeReference.CreateTypeReference<TextDecoder>(_engine)) | ||
; | ||
} | ||
private object RunTest(string source) | ||
{ | ||
return _engine.Evaluate(source).ToObject(); | ||
} | ||
|
||
[Fact] | ||
public void CanDecode() | ||
{ | ||
Assert.Equal("", RunTest($"new TextDecoder().decode()")); | ||
Assert.Equal("foo", RunTest($"new TextDecoder().decode(new Uint8Array([102,111,111]))")); | ||
Assert.Equal("foo", RunTest($"new TextDecoder().decode(new Uint8Array([102,111,111]).buffer)")); | ||
Assert.Equal("foo", RunTest($"new TextDecoder().decode(new DataView(new Uint8Array([0,102,111,111,0]).buffer,1,3))")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters