Skip to content

Commit

Permalink
Fix build warnings about obsolete methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nilproject committed Mar 28, 2023
1 parent 2538318 commit e5890f5
Show file tree
Hide file tree
Showing 23 changed files with 68 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ private void example1()
{
var context = new Context();

context.DefineVariable(_variableName).Assign(JSValue.Marshal(_value));
context.DefineVariable(_variableName).Assign(context.GlobalContext.ProxyValue(_value));

context.Eval(string.Format("console.log({0});", _variableName)); // Console: Hi, I'm value!

Expand All @@ -40,7 +40,7 @@ private void example2()
{
var context = new Context();

context.DefineVariable(_variableName).Assign(JSValue.Marshal(new ClassWithStringValue { NestedValue = _nestedValue }));
context.DefineVariable(_variableName).Assign(context.GlobalContext.ProxyValue(new ClassWithStringValue { NestedValue = _nestedValue }));

context.Eval(string.Format("console.log({0});", _variableName)); // Console: [object ClassWithStringValue]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ private void example1()
{
var context = new Context();

context.DefineVariable(_variableName).Assign(JSValue.Wrap(_value));
context.DefineVariable(_variableName).Assign(context.GlobalContext.WrapValue(_value));

context.Eval(string.Format("console.log({0});", _variableName)); // Console: [object String]

Expand All @@ -42,7 +42,7 @@ private void example2()
{
var context = new Context();

context.DefineVariable(_variableName).Assign(JSValue.Wrap(new ClassWithStringValue { NestedValue = _nestedValue }));
context.DefineVariable(_variableName).Assign(context.GlobalContext.WrapValue(new ClassWithStringValue { NestedValue = _nestedValue }));

context.Eval(string.Format("console.log({0});", _variableName)); // Console: [object ClassWithStringValue]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public override void Run()
var @delegate = new Action<string>(text => System.Windows.Forms.MessageBox.Show(text));
var context = new Context();

context.DefineVariable("alert").Assign(JSValue.Marshal(@delegate));
context.DefineVariable("alert").Assign(context.GlobalContext.ProxyValue(@delegate));
context.Eval("alert('Hello, World!')"); // Message box: Hello, World!
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public override void Run()
var objectWithEvent = new ClassWithEvent();
var context = new Context();

context.DefineVariable("objectWithEvent").Assign(JSValue.Marshal(objectWithEvent));
context.DefineVariable("objectWithEvent").Assign(context.GlobalContext.ProxyValue(objectWithEvent));
context.Eval(@"
function eventHandler(sender, eventArgs) {
console.log(eventArgs.Text);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ public override void Run()

var temp = new
{
GList = JSValue.GetGenericTypeSelector(new[]
GList = context.GlobalContext.GetGenericTypeSelector(new[]
{
typeof(List<>),
typeof(ArrayList)
})
};

context.DefineVariable("test").Assign(JSValue.Marshal(temp));
context.DefineVariable("test").Assign(context.GlobalContext.ProxyValue(temp));
context.Eval(@"
console.log('ArrayList');
var list = test.GList()();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public override void Run()
{
var context = new Context();
var instance = new TestClass();
context.DefineVariable("instance").Assign(JSValue.Wrap(instance));
context.DefineVariable("instance").Assign(context.GlobalContext.WrapValue(instance));

example1(context);

Expand Down
5 changes: 4 additions & 1 deletion NiL.JS/Core/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public IEnumerable<KeyValuePair<string, JSValue>> Variables
public class Context : IEnumerable<string>
{
[ThreadStatic]
internal static List<Context> _currentContextStack;
private static List<Context> _currentContextStack;

public static Context CurrentContext
{
Expand Down Expand Up @@ -116,6 +116,9 @@ public GlobalContext GlobalContext
{
get
{
if (this is GlobalContext gcx)
return gcx;

var iter = this;
if (iter._parent != null)
{
Expand Down
8 changes: 8 additions & 0 deletions NiL.JS/Core/GlobalContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,14 @@ public Function GetGenericTypeSelector(IList<Type> types)
return GetConstructor(type.MakeGenericType(parameters));
});
}

public JSValue WrapValue(object value)
{
if (value is null)
return JSValue.Null;

return new ObjectWrapper(value, GetPrototype(value.GetType()));
}

public JSValue ProxyValue(object value)
{
Expand Down
3 changes: 1 addition & 2 deletions NiL.JS/Extensions/ContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ namespace NiL.JS.Extensions
{
public static class ContextExtensions
{
[Obsolete("Use Add(this Context context, string key, JSValue value)")]
public static void Add(this Context context, string key, object value)
{
context.DefineVariable(key).Assign(value);
context.DefineVariable(key).Assign(context.GlobalContext.ProxyValue(value));
}

public static void Add(this Context context, string key, JSValue value)
Expand Down
14 changes: 6 additions & 8 deletions NiL.JS/Extensions/JSValueExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
using System;
using System.Reflection;
using NiL.JS.BaseLibrary;
using NiL.JS.Core;
using NiL.JS.Backward;
using System.Reflection.Emit;
using System.Linq;
using System.Linq.Expressions;
using System.ComponentModel;

namespace NiL.JS.Extensions
{
Expand Down Expand Up @@ -250,11 +244,15 @@ public static void Assign(this JSValue target, JSValue value)
{
target.Assign(value);
}
public static void Assign(this JSValue target, object value, Context context)
{
target.Assign(context.GlobalContext.ProxyValue(value));
}

[Obsolete("Use Assign(this JSValue target, JSValue value)")]
[Obsolete("Use Assign(this JSValue, JSValue) or Use Assign(this JSValue, object, Context)")]
public static void Assign(this JSValue target, object value)
{
target.Assign(JSValue.Marshal(value));
target.Assign(Context.CurrentGlobalContext.ProxyValue(value));
}

#if DEBUG && !(PORTABLE || NETCORE) // TODO
Expand Down
8 changes: 4 additions & 4 deletions Tests/BaseLibrary/JsonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,19 @@ public void Max10SpacesWhenStringifying()
[Timeout(1000)]
public void CustomSerializer()
{
var ctx = new GlobalContext();
var context = new GlobalContext();

JSValue obj = JSValue.Marshal(new Dummy());
JSValue obj = context.GlobalContext.ProxyValue(new Dummy());

ctx.ActivateInCurrentThread();
context.ActivateInCurrentThread();

try
{
Assert.IsNotNull(JSON.stringify(obj));
}
finally
{
ctx.Deactivate();
context.Deactivate();
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions Tests/Core/Functions/AsyncFunctionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ public void CompletedTaskShouldBeReturnedAsResolvedPromise()
{
var context = new Context();

context.DefineVariable("testAwaitable").Assign(JSValue.Marshal(new Func<string, Task<string>>((input) =>
context.DefineVariable("testAwaitable").Assign(new Func<string, Task<string>>((input) =>
{
return Task.FromResult(input);
})));
}), context);

context.Eval("function testAsync() { return testAwaitable('test'); }");

Expand All @@ -33,7 +33,7 @@ public async Task RejectedPromiseShouldBeReturnedAsFaultedTask()
{
var context = new Context();

context.DefineVariable("testAwaitable").Assign(JSValue.Marshal(new Func<string, Task<string>>(async (input) =>
context.DefineVariable("testAwaitable").Assign(context.GlobalContext.ProxyValue(new Func<string, Task<string>>(async (input) =>
{
await Task.Delay(500);

Expand Down Expand Up @@ -94,11 +94,11 @@ async function demo() {{

context
.DefineVariable("fetch")
.Assign(JSValue.Marshal(new Func<string, Task<string>>(FetchAsync)));
.Assign(new Func<string, Task<string>>(FetchAsync), context);

context
.DefineVariable("check")
.Assign(JSValue.Marshal(new Action<string>((value) => { Assert.AreEqual(request, value); })));
.Assign(new Action<string>((value) => { Assert.AreEqual(request, value); }), context);

context.Eval(script);

Expand Down
2 changes: 1 addition & 1 deletion Tests/Core/Functions/MethodProxyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void UndefinedToStringPropertyShouldConvertToNull()
{
var context = new Context();
var obj = new MyObject();
context.DefineVariable("obj").Assign(JSValue.Wrap(obj));
context.DefineVariable("obj").Assign(Context.CurrentGlobalContext.WrapValue(obj));

context.Eval("obj.Text = undefined");

Expand Down
12 changes: 6 additions & 6 deletions Tests/Core/JSValueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ public void MyTestMethod()
[TestMethod]
public void PrimitiveTypeShouldBeWrappedAsClass()
{
var wrappedObject = JSValue.Wrap(1);
var wrappedObject = Context.CurrentGlobalContext.WrapValue(1);

Assert.AreEqual(JSValueType.Object, wrappedObject.ValueType);
}

[TestMethod]
public void PrimitiveTypeShouldBeMarshaledAsPrimitive()
{
var wrappedObject = JSValue.Marshal(1);
var wrappedObject = Context.CurrentGlobalContext.ProxyValue(1);

Assert.AreEqual(JSValueType.Integer, wrappedObject.ValueType);
}
Expand All @@ -47,7 +47,7 @@ private enum TestEnum : byte
[TestMethod]
public void ShouldConvertStringToEnumValue()
{
var convertableJsString = JSValue.Marshal(nameof(TestEnum.Second)) as IConvertible;
var convertableJsString = Context.CurrentGlobalContext.ProxyValue(nameof(TestEnum.Second)) as IConvertible;

var enumValue = convertableJsString.ToType(typeof(TestEnum), null);

Expand All @@ -57,7 +57,7 @@ public void ShouldConvertStringToEnumValue()
[TestMethod]
public void ShouldConvertNumberInStringToEnumValue()
{
var convertableJsString = JSValue.Marshal(((int)(TestEnum.Second)).ToString()) as IConvertible;
var convertableJsString = Context.CurrentGlobalContext.ProxyValue(((int)(TestEnum.Second)).ToString()) as IConvertible;

var enumValue = convertableJsString.ToType(typeof(TestEnum), null);

Expand All @@ -67,7 +67,7 @@ public void ShouldConvertNumberInStringToEnumValue()
[TestMethod]
public void ShouldConvertIntToEnumValue()
{
var convertableJsString = JSValue.Marshal((int)(TestEnum.Second)) as IConvertible;
var convertableJsString = Context.CurrentGlobalContext.ProxyValue((int)(TestEnum.Second)) as IConvertible;

var enumValue = convertableJsString.ToType(typeof(TestEnum), null);

Expand All @@ -77,7 +77,7 @@ public void ShouldConvertIntToEnumValue()
[TestMethod]
public void ShouldReturnNullIfCannotConvert()
{
var convertableJsString = JSValue.Marshal("bazinga!") as IConvertible;
var convertableJsString = Context.CurrentGlobalContext.ProxyValue("bazinga!") as IConvertible;

var enumValue = convertableJsString.ToType(typeof(TestEnum), null);

Expand Down
6 changes: 3 additions & 3 deletions Tests/FunctionsToDelegateWrapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void TryToAddFunctionIntoListOfDelegates_Marshal()
{
var context = new Context();
var list = new List<Func<string, string>>();
context.DefineVariable("list").Assign(JSValue.Marshal(list));
context.DefineVariable("list").Assign(Context.CurrentGlobalContext.ProxyValue(list));

context.Eval("list.push(x => 'hi ' + x)"); // IList marshaled as NativeList with array-like interface

Expand All @@ -31,7 +31,7 @@ public void TryToAddFunctionIntoListOfDelegates_Wrap()
{
var context = new Context();
var list = new List<Func<string, string>>();
context.DefineVariable("list").Assign(JSValue.Wrap(list));
context.DefineVariable("list").Assign(Context.CurrentGlobalContext.WrapValue(list));

context.Eval("list.Add(x => 'hi ' + x)");

Expand Down Expand Up @@ -62,7 +62,7 @@ async function wrappedExecutesTwice() {
module
.Context
.DefineVariable("runCSharp")
.Assign(JSValue.Marshal(new Func<Task<bool>>(runCSharp)));
.Assign(module.Context.GlobalContext.ProxyValue(new Func<Task<bool>>(runCSharp)));

module.Run();

Expand Down
2 changes: 1 addition & 1 deletion Tests/Fuzz/Bug228.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void Check()
var q = new JSQuestion();
context
.DefineVariable("Q")
.Assign(JSValue.Marshal(q));
.Assign(Context.CurrentGlobalContext.ProxyValue(q));

context.DefineConstructor(typeof(Flags));

Expand Down
2 changes: 1 addition & 1 deletion Tests/Fuzz/Indexers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public string this[string i]
public void IndexersFuzz()
{
var context = new Context();
var jsval = JSValue.Marshal(new TestClass());
var jsval = Context.CurrentGlobalContext.ProxyValue(new TestClass());
context.DefineVariable("test").Assign(jsval);
context.Eval(
"var prot = test.__proto__;" +
Expand Down
2 changes: 1 addition & 1 deletion Tests/Fuzz/Int16Array.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
assert(0x0504 === array[1], 1 + " " + T.name + " from ArrayBuffer 2");

array = T(ab, 2, 1);
assert(array.length === 1, "array.length === 1 " + T.name);
assert(array.length === 1, "array.length === 1 " + T.name + " (" + array.length + ")");
assert(0x0302 === array[0], i + " " + T.name + " from ArrayBuffer 3");

assert(array[1] === undefined, "array[2] === undefined");
Expand Down
4 changes: 2 additions & 2 deletions Tests/Fuzz/fuzz.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,9 @@ var a = new A().text;
var b = new B().text;

if (a != "A")
console.log("new.target works incorrectly");
console.log("new.target works incorrectly #1");
if (b != "B")
console.log("new.target works incorrectly");
console.log("new.target works incorrectly #2");

a = console ? 1 : 2, { test: 1 };

Expand Down
8 changes: 4 additions & 4 deletions Tests/ModuleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void ImportOperatorShouldImportItem()
{
var module1 = new Module("");
var itemProperty = module1.Exports.GetType().GetProperty("Item");
itemProperty.SetValue(module1.Exports, JSValue.Marshal(0x777), new object[] { "a" });
itemProperty.SetValue(module1.Exports, Context.CurrentGlobalContext.ProxyValue(0x777), new object[] { "a" });

var module2 = new Module("module2", "import {a} from \"another module\"");
module2.ModuleResolversChain.Add(new DelegateModuleResolver((ModuleRequest request, out Module result) =>
Expand All @@ -103,7 +103,7 @@ public void DynamicImportOperatorShouldImportItem()
{
var module1 = new Module("");
var itemProperty = module1.Exports.GetType().GetProperty("Item");
itemProperty.SetValue(module1.Exports, JSValue.Marshal(0x777), new object[] { "a" });
itemProperty.SetValue(module1.Exports, Context.CurrentGlobalContext.ProxyValue(0x777), new object[] { "a" });

var module2 = new Module("module2", "var m = null; import(\"another module\").then(x => m = x)");
module2.ModuleResolversChain.Add(new DelegateModuleResolver((ModuleRequest request, out Module result) =>
Expand Down Expand Up @@ -138,7 +138,7 @@ public void DynamicImportOperatorShouldImportDefaultItem()
{
var module1 = new Module("");
var itemProperty = module1.Exports.GetType().GetProperty("Item");
itemProperty.SetValue(module1.Exports, JSValue.Marshal(0x777), new object[] { string.Empty });
itemProperty.SetValue(module1.Exports, Context.CurrentGlobalContext.ProxyValue(0x777), new object[] { string.Empty });

var module2 = new Module("module2", "var m = null; import(\"another module\").then(x => m = x)");
module2.ModuleResolversChain.Add(new DelegateModuleResolver((ModuleRequest request, out Module result) =>
Expand All @@ -159,7 +159,7 @@ public void DynamicImportOperatorShouldImportDefaultItem()
{
Thread.Sleep(1);
var imported = module2.Context.GetVariable("m");
if (!imported.Defined)
if (!imported.Defined || imported.IsNull)
continue;

if (Equals(imported["default"].Value, 0x777))
Expand Down
Loading

0 comments on commit e5890f5

Please sign in to comment.