/// <summary> /// This method creates a new <see cref="JavaScriptValue"/> representing a JavaScript function, and /// performs the binding of the resulting function with the native host function. This ensures that the /// host function will not be garbage collected as long as the JavaScript function has not been garbage /// collected. /// </summary> /// <param name="func">The host function used to create the JavaScript function and binding.</param> public JavaScriptValue BindFunction(JavaScriptNativeFunction func) { return(_scope.Run(() => { var jsFunc = JsSafeDecorator.Decorate(func); var jsValue = JavaScriptValue.CreateFunction(jsFunc); Link(jsValue, jsFunc); return jsValue; })); }
/// <summary> /// Creates the <see cref="JsBinding"/> instance representing the new bound JavaScript object. /// </summary> public JsBinding Build() { return(_scope.Run(() => { // Create a host object binding or new JS Object binding var jsValue = null != _boundTo ? _binder.BindObject(_boundTo) : JavaScriptValue.CreateObject(); var binding = new JsBinding(_scope, _binder, _interop, jsValue); // Bind Host Object Methods, Properties, and Fields if bound to host object if (null != _boundTo && null != _hostType) { BindMethods(binding, _hostType, _boundTo); BindProperties(binding, _hostType, _boundTo); BindFields(binding, _hostType, _boundTo); } // Set custom binding values if (null != _values) { for (int i = 0; i < _values.Count; ++i) { var valueDescriptor = _values[i]; binding.SetValue(valueDescriptor.Name, valueDescriptor.Value, valueDescriptor.ValueType); } } return binding; })); }
/// <summary> /// This method binds a function to a specific field on a JavaScript object. /// </summary> /// <param name="name">The name of the field.</param> /// <param name="jsFunction">The callback function to execute when the JavaScript function is invoked.</param> /// <param name="instanceData">Any instance data that should be passed when the function is executed.</param> public void AddInstanceFunction(string name, JavaScriptNativeFunction jsFunction, IntPtr instanceData) { _scope.Run(() => { var jsValue = _binder.BindInstanceFunction(jsFunction, instanceData); _value.SetProperty(JavaScriptPropertyId.FromString(name), jsValue, true); }); }
/// <inheritdoc /> public object Apply(object @this, params object[] args) { return(_scope.Run(() => { JavaScriptValue[] jsValues = new JavaScriptValue[1 + args.Length]; if (_binding.IsValid) { jsValues[0] = _binding; } else { jsValues[0] = null != @this ? _interop.ToJsObject(@this, @this.GetType()) : JavaScriptValue.Undefined; } for (var i = 0; i < args.Length; ++i) { var arg = args[i]; jsValues[i + 1] = null != arg ? _interop.ToJsObject(arg, arg.GetType()) : JavaScriptValue.Null; } return TryInvoke(jsValues); })); }
/// <summary> /// Creates a new <see cref="JsModule"/> instance. /// </summary> public JsModule(JsContextScope scope, JsBinder binder, JsInterop interop, string moduleId) { _scope = scope; _binder = binder; _interop = interop; ModuleId = moduleId; // Create JS Representation Module = _scope.Run(() => { var jsValue = JavaScriptValue.CreateObject(); jsValue.AddRef(); return(new JsBinding(_scope, _binder, _interop, jsValue)); }); }
/// <inheritdoc/> public T GetExportedValue <T>(string name) { return(_scope.Run(() => { if (null == _exports) { if (!Module.HasValue("exports")) { return default(T); } var exports = Module.GetValue("exports"); _exports = new JsBinding(_scope, _binder, _interop, exports); } return _exports.GetValue <T>(name); })); }