Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Microsoft.Scripting/HostBridge/BridgeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public BridgeManager(JavaScriptEngine engine)
classBridges_ = new Dictionary<Type, ClassBridge>();
}

public ClassBridge GetBridge(Type type)
public ClassBridge GetBridge(Type type, TaskFactory taskFactory)
{
ClassBridge result;
if (classBridges_.TryGetValue(type, out result))
Expand All @@ -37,7 +37,7 @@ public ClassBridge GetBridge(Type type)
}
else
{
result = new ClassBridge(type, this);
result = new ClassBridge(type, this, taskFactory);
classBridges_.Add(type, result);
}

Expand Down
16 changes: 8 additions & 8 deletions src/Microsoft.Scripting/HostBridge/ClassBridge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal class ClassBridge
private BridgeManager manager_;
private HostClassMode hostMode_;

public ClassBridge(Type type, BridgeManager manager)
public ClassBridge(Type type, BridgeManager manager, TaskFactory taskFactory)
{
Debug.Assert(type != null && manager != null);
if (type == null)
Expand Down Expand Up @@ -54,7 +54,7 @@ public ClassBridge(Type type, BridgeManager manager)
instanceMethods_ = new List<MethodModel>();
staticMethods_ = new List<MethodModel>();

InitializeBridge();
InitializeBridge(taskFactory);
}

internal void AddRef()
Expand Down Expand Up @@ -92,15 +92,15 @@ private bool ShouldProjectMember(MemberInfo member)
member.GetCustomAttribute<JavaScriptHostMemberAttribute>() != null);
}

private void InitializeBridge()
private void InitializeBridge(TaskFactory taskFactory)
{
ClassBridge baseTypeBridge = null;
if (typeInfo_.BaseType != null)
{
if (hostMode_ == HostClassMode.FullClass ||
(hostMode_ == HostClassMode.OptIn && IsOptedIn(typeInfo_.BaseType)))
{
baseTypeBridge = manager_.GetBridge(typeInfo_.BaseType);
baseTypeBridge = manager_.GetBridge(typeInfo_.BaseType, taskFactory);
}
}

Expand Down Expand Up @@ -146,12 +146,12 @@ private void InitializeBridge()

foreach (var methodGroup in instanceMethods)
{
BridgeMethod(engine, methodGroup.ToArray(), true, Prototype);
BridgeMethod(engine, methodGroup.ToArray(), true, Prototype, taskFactory);
}

foreach (var methodGroup in staticMethods)
{
BridgeMethod(engine, methodGroup.ToArray(), false, Constructor);
BridgeMethod(engine, methodGroup.ToArray(), false, Constructor, taskFactory);
}

// todo: events
Expand Down Expand Up @@ -180,7 +180,7 @@ private void BridgeProperty(JavaScriptEngine engine, PropertyInfo property, bool
}
}

private void BridgeMethod(JavaScriptEngine engine, MethodInfo[] methodGroup, bool isInstance, JavaScriptObject targetObject)
private void BridgeMethod(JavaScriptEngine engine, MethodInfo[] methodGroup, bool isInstance, JavaScriptObject targetObject, TaskFactory taskFactory)
{
MethodModel methodModel;

Expand All @@ -192,7 +192,7 @@ private void BridgeMethod(JavaScriptEngine engine, MethodInfo[] methodGroup, boo
{
// todo: enable async to have names
// fn = engine.CreateFunction(methodModel.AsyncEntryPoint, methodModel.FullName, AsyncHostFunctionKind.Promise);
fn = engine.CreateFunction(methodModel.AsyncEntryPoint, AsyncHostFunctionKind.Promise);
fn = engine.CreateFunction(methodModel.AsyncEntryPoint, taskFactory, AsyncHostFunctionKind.Promise);
}
else
{
Expand Down
9 changes: 7 additions & 2 deletions src/Microsoft.Scripting/JavaScript/JavaScriptConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public JavaScriptValue FromObject(object o)
}
}

public JavaScriptValue FromObjectViaNewBridge(object o)
public JavaScriptValue FromObjectViaNewBridge(object o, TaskFactory taskFactory)
{
var eng = GetEngine();
if (o == null)
Expand Down Expand Up @@ -329,11 +329,16 @@ public JavaScriptValue FromObjectViaNewBridge(object o)
}
else
{
ClassBridge cb = hostBridge_.GetBridge(t);
ClassBridge cb = hostBridge_.GetBridge(t, taskFactory);
return cb.ProjectObject(o);
}
}

public JavaScriptValue FromObjectViaNewBridge(object o)
{
return FromObjectViaNewBridge(o, Task.Factory);
}

public object ToObject(JavaScriptValue val)
{
switch (val.Type)
Expand Down
13 changes: 10 additions & 3 deletions src/Microsoft.Scripting/JavaScript/JavaScriptEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -671,8 +671,8 @@ public JavaScriptFunction CreateFunction(JavaScriptCallableFunction hostFunction

return CreateObjectFromHandle(resultHandle) as JavaScriptFunction;
}

public JavaScriptFunction CreateFunction(JavaScriptCallableAsyncFunction hostFunction, AsyncHostFunctionKind functionMarshalingKind = AsyncHostFunctionKind.Promise)
public JavaScriptFunction CreateFunction(JavaScriptCallableAsyncFunction hostFunction, TaskFactory taskFactory, AsyncHostFunctionKind functionMarshalingKind = AsyncHostFunctionKind.Promise)
{
if (hostFunction == null)
throw new ArgumentNullException(nameof(hostFunction));
Expand Down Expand Up @@ -708,7 +708,9 @@ public JavaScriptFunction CreateFunction(JavaScriptCallableAsyncFunction hostFun

var promise = Promise_.Construct(new[] { promiseInit });

Task.Run(async () =>
var runner = (taskFactory == Task.Factory ? (Func < Func<Task>, Task> )Task.Run : taskFactory.StartNew);

runner(async () =>
{
JavaScriptValue resultValue = null;
bool succeeded = false;
Expand Down Expand Up @@ -768,6 +770,11 @@ public JavaScriptFunction CreateFunction(JavaScriptCallableAsyncFunction hostFun
return CreateObjectFromHandle(resultHandle) as JavaScriptFunction;
}

public JavaScriptFunction CreateFunction(JavaScriptCallableAsyncFunction hostFunction, AsyncHostFunctionKind functionMarshalingKind = AsyncHostFunctionKind.Promise)
{
return CreateFunction(hostFunction, Task.Factory, functionMarshalingKind);
}

public JavaScriptFunction CreateFunction(JavaScriptCallableAsyncFunction hostFunction, string name, AsyncHostFunctionKind functionMarshalingKind = AsyncHostFunctionKind.Promise)
{
return null;
Expand Down
1 change: 1 addition & 0 deletions src/Microsoft.Scripting/Microsoft.Scripting.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Compile Include="Errors.cs" />
Expand Down