コード例 #1
0
        public JurassicJsEngine(object global)
        {
            _typeConverter = new ClrTypeConverter(new ScriptEngine(), () => _global);

            if (global != null)
            {
	            _global = global;
	            Engine.Global.DefineProperties(_typeConverter, global.GetType(), global);
	            foreach (var method in _global.GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance))
	            {
		            var ctor = method.GetCustomAttribute<JsCtorAttribute>();
		            if(ctor == null)
			            continue;

		            var paramConverter = ClrTypeConverter.GetParamsConverter(_typeConverter, method.GetParameters());
		            
		            var prototype = _typeConverter.GetPrototype(method.ReturnType);
		            var jsCtor = new ClrCtor(_typeConverter, method.ReturnType, prototype, ctor.Name, args =>
		            {
			            var clrArgs = paramConverter(Engine.Global, args);
			            return method.Invoke(global, clrArgs);
		            });
            
		            _typeConverter.Engine.SetGlobalValue(ctor.Name, jsCtor);
	            }
            }
        }
コード例 #2
0
        public void AddGlobalType(Type type)
        {
            var jsTypeName = type.GetCustomAttribute<JsNameAttribute>()?.Name ?? type.Name;
            
            var prototype = _typeConverter.GetPrototype(type);
            
            var ctors = type.GetConstructors(BindingFlags.Public | BindingFlags.Instance).OrderByDescending(x => x.GetParameters().Length).ToArray();

            if (ctors.Length > 0)
            {
                var jsCtor = new ClrCtor(_typeConverter, type, prototype,jsTypeName, args =>
                {
                    var ctor = ctors.FirstOrDefault(x => x.GetParameters().IsAppropriate(args));
                    if(ctor == null)
	                    throw new Exception("Appropriate constructor not found.");
                    
                    var ctorParameters = ctor.GetParameters();
                    var clrArgs = _typeConverter.ConvertParametersToClr(ctorParameters, null, args);
                    return ctor.Invoke(clrArgs);
                });
                
                _typeConverter.Engine.SetGlobalValue(jsTypeName, jsCtor);
            }
            else
            {
                //todo: fix error
                var jsCtor = new ClrCtor(_typeConverter, type, prototype,jsTypeName, _ => throw new Exception("Error"));
                _typeConverter.Engine.SetGlobalValue(jsTypeName, jsCtor);
            }
        }
コード例 #3
0
 public void AddGlobalType(Type type, string jsTypeName, Type[] argumentsTypes, Func<object[], object> func)
 {
     var prototype = _typeConverter.GetPrototype(type);
     
     var jsCtor = new ClrCtor(_typeConverter, type, prototype, jsTypeName, args =>
     {
         var clrArgs = _typeConverter.ConvertParametersToClr(argumentsTypes, null, args);
         return func(clrArgs);
     });
     
     _typeConverter.Engine.SetGlobalValue(jsTypeName, jsCtor);
 }