public MarkdownService() { _ctx = new JSContext(_vm); var script = System.IO.File.ReadAllText("Markdown/marked.js", System.Text.Encoding.UTF8); _ctx.EvaluateScript(script); _val = _ctx[new NSString("marked")]; }
public static JSValue GlobalCode(JSContext context) { context.ReturnValue = JSUndefined.Instance; context.CreateMutableBinding("__func"); context.SetVarBinding("__func", context.GetFunctionReference(0)); context.ReturnValue = JSValue.JSEqualsExact(context.GetBindingValue("__func")["prototype"]["constructor"], context.GetBindingValue("__func")); return context.ReturnValue; }
public string ConvertMarkdown(string c) { if (string.IsNullOrEmpty(c)) return string.Empty; using (var vm = new JSVirtualMachine()) { var ctx = new JSContext(vm); var script = System.IO.File.ReadAllText("Markdown/marked.js", System.Text.Encoding.UTF8); ctx.EvaluateScript(script); var val = ctx[new NSString("marked")]; return val.Call(JSValue.From(c, ctx)).ToString(); } }
public static Task<Stage> Load(string scriptName) { return Task.Factory.StartNew(()=>{ var script = File.ReadAllText (scriptName); if(script.Contains("( function($, Edge, compId){")) script = script.Replace("( function($, Edge, compId){",""); var endIndex= script.IndexOf("Edge.registerC"); if(endIndex > 0) script = script.Substring(0,endIndex); script += Environment.NewLine + "var js = JSON.stringify(symbols);"; var context = new JSContext(); var test = context.EvaluateScript (script); var result = context[(NSString)"js"].ToString(); result = result.Replace("\"${_", "\"").Replace("}\"","\""); var stage = StageParser.Parse(result); return stage; }); }
public void Initialize() { testFunctionsMock = new Mock<TestFunctions>(); Context = new JSContext(); Context.GetGlobalObject().SetProperty("simpleProperties", new SimpleProperties() { stringProperty = "stringPropertyValue", intProperty = 3, floatProperty = (float)Math.PI, doubleProperty = GOLDEN_RATIO, boolProperty = true, floatsProperty = new float[] { 3.14f, 16/9 }, dictProperty = new Dictionary<object,object>() { {"123", 2}, {"string", "hello world"} }, nonGenericDictionary = new Dictionary<string, object>() { { "x", 1 } }, nestedProperty = testFunctionsMock.Object }); Context.GetGlobalObject().SetProperty("testFunctions", testFunctionsMock.Object); }
// Created by JavaScript runtime only public DomAttribute(JSContext ctxt) { }
public bool IsContext(JSContext ctx) { return(ctx == _ctx); }
public void ReleaseJSValues() { if (!_this_obj.IsNullish()) { JSApi.JS_FreeValue(_ctx, _startFunc); _startFunc = JSApi.JS_UNDEFINED; _startValid = false; JSApi.JS_FreeValue(_ctx, _resetFunc); _resetFunc = JSApi.JS_UNDEFINED; _resetValid = false; JSApi.JS_FreeValue(_ctx, _onEnableFunc); _onEnableFunc = JSApi.JS_UNDEFINED; _onEnableValid = false; JSApi.JS_FreeValue(_ctx, _onDisableFunc); _onDisableFunc = JSApi.JS_UNDEFINED; _onDisableValid = false; JSApi.JS_FreeValue(_ctx, _onApplicationFocusFunc); _onApplicationFocusFunc = JSApi.JS_UNDEFINED; _onApplicationFocusValid = false; #if UNITY_EDITOR JSApi.JS_FreeValue(_ctx, _onDrawGizmosFunc); _onDrawGizmosFunc = JSApi.JS_UNDEFINED; _onDrawGizmosValid = false; #endif JSApi.JS_FreeValue(_ctx, _onApplicationPauseFunc); _onApplicationPauseFunc = JSApi.JS_UNDEFINED; _onApplicationPauseValid = false; JSApi.JS_FreeValue(_ctx, _onApplicationQuitFunc); _onApplicationQuitFunc = JSApi.JS_UNDEFINED; _onApplicationQuitValid = false; JSApi.JS_FreeValue(_ctx, _onDestroyFunc); _onDestroyFunc = JSApi.JS_UNDEFINED; _onDestroyValid = false; JSApi.JS_FreeValue(_ctx, _awakeFunc); _awakeFunc = JSApi.JS_UNDEFINED; _awakeValid = false; JSApi.JS_FreeValue(_ctx, _onBeforeSerializeFunc); _onBeforeSerializeFunc = JSApi.JS_UNDEFINED; _onBeforeSerializeValid = false; JSApi.JS_FreeValue(_ctx, _onAfterDeserializeFunc); _onAfterDeserializeFunc = JSApi.JS_UNDEFINED; _onAfterDeserializeValid = false; this.OnUnbindingJSFuncs(); JSApi.JS_FreeValue(_ctx, _this_obj); _this_obj = JSApi.JS_UNDEFINED; } _isScriptInstanced = false; var context = ScriptEngine.GetContext(_ctx); _ctx = JSContext.Null; if (context != null) { context.OnDestroy -= OnContextDestroy; #if UNITY_EDITOR context.OnScriptReloading -= OnScriptReloading; context.OnScriptReloaded -= OnScriptReloaded; #endif } }
public Center(JSContext ctxt) : base(ctxt) { }
// Created by JavaScript runtime only public StyleSheetRule(JSContext ctxt) { }
// Created by JavaScript runtime only public History(JSContext ctxt) { }
public FixedWidthFont(JSContext ctxt) : base(ctxt) { }
public Emphasized(JSContext ctxt) : base(ctxt) { }
// 自动判断类型 //TODO: use type-indexed handler map // [MethodImpl(MethodImplOptions.AggressiveInlining)] public static JSValue js_push_var(JSContext ctx, object o) { if (o == null) { return(JSApi.JS_UNDEFINED); } var type = o.GetType(); //NOTE: 1. push as simple types if (type.IsValueType) { if (type.IsPrimitive) { if (type == typeof(bool)) { return(js_push_primitive(ctx, (bool)o)); } if (type == typeof(byte)) { return(js_push_primitive(ctx, (byte)o)); } if (type == typeof(char)) { return(js_push_primitive(ctx, (char)o)); } if (type == typeof(sbyte)) { return(js_push_primitive(ctx, (sbyte)o)); } if (type == typeof(short)) { return(js_push_primitive(ctx, (short)o)); } if (type == typeof(ushort)) { return(js_push_primitive(ctx, (ushort)o)); } if (type == typeof(int)) { return(js_push_primitive(ctx, (int)o)); } if (type == typeof(uint)) { return(js_push_primitive(ctx, (uint)o)); } if (type == typeof(long)) { return(js_push_primitive(ctx, (long)o)); } if (type == typeof(ulong)) { return(js_push_primitive(ctx, (ulong)o)); } if (type == typeof(float)) { return(js_push_primitive(ctx, (float)o)); } if (type == typeof(double)) { return(js_push_primitive(ctx, (double)o)); } } else { if (type.IsEnum) { return(js_push_primitive(ctx, Convert.ToInt32(o))); } } } else { if (type == typeof(string)) { return(js_push_primitive(ctx, (string)o)); } } //NOTE: 2. fallthrough, push as object return(js_push_classvalue(ctx, o)); }
protected static JSValue _js_game_object_get_component(JSContext ctx, JSValue ctor, Component component, Type type) { return(_js_game_object_get_component(ctx, ctor, component.gameObject, type)); }
protected static JSValue _js_game_object_get_components_in_parent(JSContext ctx, JSValue ctor, Component component, Type type, bool includeInactive) { return(_js_game_object_get_components_in_parent(ctx, ctor, component.gameObject, type, includeInactive)); }
protected static JSValue _js_game_object_get_components(JSContext ctx, JSValue ctor, Component component, Type type, List <Component> results) { return(_js_game_object_get_components(ctx, ctor, component.gameObject, type, results)); }
public Select(JSContext ctxt) : base(ctxt) { }
private string js_source_position(JSContext ctx, string funcName, string fileName, int lineNumber) { return($"{funcName} ({fileName}:{lineNumber})"); }
// Created by JavaScript runtime only public DomImplementation(JSContext ctxt) { }
// Constructed by JavaScript runtime only public SelectionObject(JSContext ctxt) { }
private static bool js_value_cast_void(JSContext ctx, JSValue val, out object o) { o = null; return(true); }
public Meta(JSContext ctxt) : base(ctxt) { }
public void Init() { context = new JSContext(); }
public Deleted(JSContext ctxt) : base(ctxt) { }
public override JSValue Invoke(JSContext ctx, JSValue this_obj, int argc, JSValue[] argv) { return(Values._js_crossbind_constructor(ctx, this_obj)); }
private void CreateWindowScriptObject(JSContext context) { if (ObjectForScripting != null && context != null) { JSObject global = context.GetGlobalObject(); JSObject window = global.GetProperty("window") as JSObject; if (window != null) window.SetProperty("external", (object)ObjectForScripting); } }
public Heading3(JSContext ctxt) : base(ctxt) { }
public XmlNode(JSContext ctxt) { }
// Constructed by JavaScript runtime only public XmlParseError(JSContext ctxt) { }
public override bool CheckArgs(JSContext ctx, int argc, JSValue[] argv) { return(true); }
// Constructed by JavaScript runtime only public AreaCollection(JSContext ctxt) { }
public Ruby(JSContext ctxt) : base(ctxt) { }
public JSValue Invoke(JSContext ctx, JSValue this_obj, int argc, JSValue[] argv) { return(_method.Invoke(ctx, this_obj, argc, argv)); }
public Font(JSContext ctxt) : base(ctxt) { }
public override JSValue Invoke(JSContext ctx, JSValue this_obj, int argc, JSValue[] argv) { if (!_methodInfo.IsPublic && !_type.privateAccess) { return(JSApi.JS_ThrowInternalError(ctx, "method is inaccessible due to its protection level")); } object self = null; if (!_methodInfo.IsStatic) { if (!Values.js_get_var(ctx, this_obj, _type.type, out self) || !_type.CheckThis(self)) { throw new ThisBoundException(); } } var nArgs = _methodParameters.Length; var args = new object[nArgs]; var vIndex = 0; var bBackValues = false; for (var i = 0; i < nArgs; i++) { var parameterInfo = _methodParameters[i]; var pType = parameterInfo.ParameterType; if (Values.IsContextualType(pType)) { args[i] = Values.js_get_context(ctx, pType); } else { if (_isVarargMethod && i == nArgs - 1) { var varArgLength = argc - nArgs + 1; var varArgType = pType.GetElementType(); var varArgArray = Array.CreateInstance(varArgType, varArgLength); for (var varArgIndex = 0; varArgIndex < varArgLength; varArgIndex++) { object varArgElement = null; if (!Values.js_get_var(ctx, argv[vIndex++], varArgType, out varArgElement)) { return(JSApi.JS_ThrowInternalError(ctx, $"failed to cast val (vararg {varArgIndex})")); } varArgArray.SetValue(varArgElement, varArgIndex); } args[i] = varArgArray; } else { if (pType.IsByRef) { bBackValues = true; if (!parameterInfo.IsOut) { if (!Values.js_get_var(ctx, argv[vIndex], pType.GetElementType(), out args[i])) { return(JSApi.JS_ThrowInternalError(ctx, $"failed to cast val {vIndex}")); } } } else { if (!Values.js_get_var(ctx, argv[vIndex], pType, out args[i])) { return(JSApi.JS_ThrowInternalError(ctx, $"failed to cast val {vIndex}")); } } vIndex++; } } } var ret = _methodInfo.Invoke(self, args); if (bBackValues) { vIndex = 0; for (var i = 0; i < nArgs; i++) { var parameterInfo = _methodParameters[i]; var pType = parameterInfo.ParameterType; if (!Values.IsContextualType(pType)) { if (_isVarargMethod && i == nArgs - 1) { } else { if (pType.IsByRef) { var backValue = Values.js_push_var(ctx, args[i]); var valueAtom = ScriptEngine.GetContext(ctx).GetAtom("value"); JSApi.JS_SetProperty(ctx, argv[vIndex], valueAtom, backValue); } vIndex++; } } } } if (_type.type.IsValueType && !_methodInfo.IsStatic) { Values.js_rebind_var(ctx, this_obj, _type.type, self); } if (_methodInfo.ReturnType != typeof(void)) { return(Values.js_push_var(ctx, ret)); } return(JSApi.JS_UNDEFINED); }
public Inserted(JSContext ctxt) : base(ctxt) { }
public JSObject(JSContext ctxt) { }
public Span(JSContext ctxt) : base(ctxt) { }
public Italic(JSContext ctxt) : base(ctxt) { }
// Constructed by JavaScript runtime only public XmlNotation(JSContext ctxt) : base(ctxt) { }
public static JSValue PrimitiveCall(JSContext ctx, JSValue this_obj, int argc, JSValue[] argv) { Debug.Log("直接无包装导出方法"); return(JSApi.JS_UNDEFINED); }
// Constructed by JavaScript runtime only public XmlDocumentFragment(JSContext ctxt) : base(ctxt) { }
public static bool js_rebind_this(JSContext ctx, JSValue this_obj, ref Color32 o) { return(JSApi.jsb_set_byte_4(this_obj, o.r, o.g, o.b, o.a) == 1); }
// Constructed by JavaScript runtime only public XmlDocumentType(JSContext ctxt) : base(ctxt) { }
// private MethodBase _methodBase; public abstract bool CheckArgs(JSContext ctx, int argc, JSValue[] argv);
public Variable(JSContext ctxt) : base(ctxt) { }
public abstract JSValue Invoke(JSContext ctx, JSValue this_obj, int argc, JSValue[] argv);
// Constructed by JavaScript runtime only public RenderStyle(JSContext ctxt) { }
public Big(JSContext ctxt) : base(ctxt) { }
public void Init() { context = new JSContext (); }
public static bool js_rebind_this(JSContext ctx, JSValue this_obj, ref Vector3Int o) { return(JSApi.jsb_set_int_3(this_obj, o.x, o.y, o.z) == 1); }
public static JSValue __func_0(JSContext context) { return JSUndefined.Instance; }
/// <summary> /// 从 JSValue 反推 Delegate, JSValue 可能是一个 js function, cs delegate (js object) <br/> /// 注意: 会自动创建 ScriptDelegate 映射 /// </summary> public static bool js_get_delegate(JSContext ctx, JSValue val, Type delegateType, out Delegate o) { if (val.IsNullish()) { o = null; return(true); } // 检查 val 是否是一个委托对象 wrapped object if (JSApi.JS_IsObject(val)) { if (js_get_classvalue <Delegate>(ctx, val, out o)) { return(o == null || o.GetType() == delegateType); } if (JSApi.JS_IsFunction(ctx, val) == 1) { ScriptDelegate fn; var cache = ScriptEngine.GetObjectCache(ctx); if (cache.TryGetDelegate(val, out fn)) { // 已经存在映射关系, 找出符合预期类型的委托 o = fn.Match(delegateType); if (o == null) { // 存在 JSValue => Delegate 的多重映射 var types = ScriptEngine.GetTypeDB(ctx); var func = types.GetDelegateFunc(delegateType); o = Delegate.CreateDelegate(delegateType, fn, func, false); if (o != null) { fn.Add(o); } } return(o != null); } else { // 建立新的映射关系 var context = ScriptEngine.GetContext(ctx); var types = context.GetTypeDB(); var func = types.GetDelegateFunc(delegateType); if (func == null) { o = null; return(false); } fn = new ScriptDelegate(context, val); o = Delegate.CreateDelegate(delegateType, fn, func, false); if (o != null) { fn.Add(o); } return(o != null); } } } o = null; return(false); }
public Quote(JSContext ctxt) : base(ctxt) { }
public static bool js_rebind_this(JSContext ctx, JSValue this_obj, ref Vector4 o) { return(JSApi.jsb_set_float_4(this_obj, o.x, o.y, o.z, o.w) == 1); }
// Constructed by JavaScript runtime only public XmlEntity(JSContext ctxt) : base(ctxt) { }
public Colgroup(JSContext ctxt) : base(ctxt) { }
public SoftLineBreak(JSContext ctxt) : base(ctxt) { }