-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add abstract JS module and path.join
- Loading branch information
Showing
6 changed files
with
108 additions
and
118 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
This file was deleted.
Oops, something went wrong.
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,73 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using ChakraHost.Hosting; | ||
using System.Diagnostics; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace Electrino.JS | ||
{ | ||
abstract class AbstractJSModule | ||
{ | ||
private JavaScriptValue module; | ||
private string id; | ||
|
||
public static void AttachModule(JavaScriptValue module, AbstractJSModule subModule) | ||
{ | ||
Debug.Assert(Native.JsSetProperty(module, JavaScriptPropertyId.FromString(subModule.GetId()), | ||
subModule.GetModule(), false) == JavaScriptErrorCode.NoError, "Failed to attach module"); | ||
} | ||
|
||
public static void AttachModule(AbstractJSModule module, AbstractJSModule subModule) | ||
{ | ||
AttachModule(module.GetModule(), subModule); | ||
} | ||
|
||
private static void AttachMethod(AbstractJSModule module, JavaScriptNativeFunction method, string id) | ||
{ | ||
JavaScriptValue requireToString; | ||
Debug.Assert(Native.JsCreateFunction(method, IntPtr.Zero, out requireToString) == JavaScriptErrorCode.NoError, "Failed to create method"); | ||
|
||
Debug.Assert(Native.JsSetProperty(module.GetModule(), JavaScriptPropertyId.FromString(id), requireToString, false) | ||
== JavaScriptErrorCode.NoError, "Failed to define tostring on require"); | ||
} | ||
|
||
public static string JSValToString(JavaScriptValue val) | ||
{ | ||
val = val.ConvertToString(); | ||
IntPtr returnValue; | ||
UIntPtr stringLength; | ||
Debug.Assert(Native.JsStringToPointer(val, out returnValue, out stringLength) == JavaScriptErrorCode.NoError, "Failed to convert return value."); | ||
return Marshal.PtrToStringUni(returnValue); | ||
} | ||
|
||
public AbstractJSModule(string id) | ||
{ | ||
this.id = id; | ||
|
||
Debug.Assert(Native.JsCreateObject(out module) == JavaScriptErrorCode.NoError, "Failed to create module"); | ||
} | ||
|
||
public void AttachModule(AbstractJSModule subModule) | ||
{ | ||
AttachModule(this, subModule); | ||
} | ||
|
||
public void AttachMethod(JavaScriptNativeFunction method, string id) | ||
{ | ||
AttachMethod(this, method, id); | ||
} | ||
|
||
public JavaScriptValue GetModule() | ||
{ | ||
return module; | ||
} | ||
|
||
public string GetId() | ||
{ | ||
return id; | ||
} | ||
} | ||
} |
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,27 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using ChakraHost.Hosting; | ||
|
||
namespace Electrino.JS | ||
{ | ||
class JSPath : JS.AbstractJSModule | ||
{ | ||
public JSPath(): base("path") | ||
{ | ||
AttachMethod(Join, "join"); | ||
} | ||
|
||
private static JavaScriptValue Join(JavaScriptValue callee, bool isConstructCall, JavaScriptValue[] arguments, ushort argumentCount, IntPtr callbackData) | ||
{ | ||
string[] args = new string[arguments.Length - 1]; | ||
for (int i = 1; i < arguments.Length; i++) | ||
{ | ||
args[i - 1] = JSValToString(arguments[i]); | ||
} | ||
return JavaScriptValue.FromString(String.Join("\\", args)); | ||
} | ||
} | ||
} |
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