Skip to content

Commit

Permalink
Add abstract JS module and path.join
Browse files Browse the repository at this point in the history
  • Loading branch information
tht13 committed May 15, 2017
1 parent b062176 commit d5a5509
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 118 deletions.
2 changes: 1 addition & 1 deletion Electrino/win10/Electrino/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public App()
this.InitializeComponent();
this.Suspending += OnSuspending;
System.Diagnostics.Debug.WriteLine(jsApp.init());
System.Diagnostics.Debug.WriteLine(jsApp.runScript("require.toString()"));
System.Diagnostics.Debug.WriteLine(jsApp.runScript("path.join('blah', 'foo');"));
}

/// <summary>
Expand Down
111 changes: 0 additions & 111 deletions Electrino/win10/Electrino/ChakraHost.cs

This file was deleted.

3 changes: 2 additions & 1 deletion Electrino/win10/Electrino/Electrino.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@
<Compile Include="App.xaml.cs">
<DependentUpon>App.xaml</DependentUpon>
</Compile>
<Compile Include="ChakraHost.cs" />
<Compile Include="Hosting\JavaScriptBackgroundWorkItemCallback.cs" />
<Compile Include="Hosting\JavaScriptBeforeCollectCallback.cs" />
<Compile Include="Hosting\JavaScriptContext.cs" />
Expand Down Expand Up @@ -126,6 +125,8 @@
<Compile Include="Hosting\JavaScriptValueType.cs" />
<Compile Include="Hosting\Native.cs" />
<Compile Include="JavaScriptApp.cs" />
<Compile Include="JS\JSModule.cs" />
<Compile Include="JS\JSPath.cs" />
<Compile Include="MainPage.xaml.cs">
<DependentUpon>MainPage.xaml</DependentUpon>
</Compile>
Expand Down
73 changes: 73 additions & 0 deletions Electrino/win10/Electrino/JS/JSModule.cs
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;
}
}
}
27 changes: 27 additions & 0 deletions Electrino/win10/Electrino/JS/JSPath.cs
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));
}
}
}
10 changes: 5 additions & 5 deletions Electrino/win10/Electrino/JavaScriptApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ public string init()
if (Native.JsSetProperty(jsAppGlobalObject, JavaScriptPropertyId.FromString("require"), require, false) != JavaScriptErrorCode.NoError)
return "failed to define require on global";

JS.AbstractJSModule.AttachModule(jsAppGlobalObject, new JS.JSPath());


JavaScriptValue requireToString;
JavaScriptNativeFunction echoDelegate = Echo;
if (Native.JsCreateFunction(echoDelegate, IntPtr.Zero, out requireToString) != JavaScriptErrorCode.NoError)
if (Native.JsCreateFunction(toString, IntPtr.Zero, out requireToString) != JavaScriptErrorCode.NoError)
return "failed to create require toString function";
if (Native.JsSetProperty(require, JavaScriptPropertyId.FromString("toString"), requireToString, false) != JavaScriptErrorCode.NoError)
return "failed to define tostring on require";
Expand All @@ -71,10 +72,9 @@ public string init()
return "NoError";
}

private static JavaScriptValue Echo(JavaScriptValue callee, bool isConstructCall, JavaScriptValue[] arguments, ushort argumentCount, IntPtr callbackData)
private static JavaScriptValue toString(JavaScriptValue callee, bool isConstructCall, JavaScriptValue[] arguments, ushort argumentCount, IntPtr callbackData)
{
System.Diagnostics.Debug.WriteLine("require.tostring called");
return JavaScriptValue.Invalid;
return JavaScriptValue.FromString("Require module");
}

public string runScript(string script)
Expand Down

0 comments on commit d5a5509

Please sign in to comment.