[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[技术讨论] 关于jscript.net

本帖最后由 jyswjjgdwtdtj 于 2024-8-26 15:23 编辑

jscript.net是c#,vb.net的同胞兄弟 然而c#没有历史包袱,如今愈发茁壮,vb.net尽管语法似乎不太适配如今的新东西,但依然有着大量的爱好者
jscript.net却被M$彻底遗弃,早早夭折,typescript这个同为js系的弟弟彻底为他添上坟头最后一捧土
说到底,jscript.net从未被当成一项正经的语言 它不支持直接使用win32api操作底层(哪怕对于这项语言的设计者来说在容易不过),甚至不像powershell可以简单的add-type加载c#Dll进assembly 它不支持泛型(哪怕它的标准es4中有)……
js.net的定位非M$声称的在asp.net里做vbs的后继者,因为他比起c#来说一无是处,而似乎更像powershell的脚本语言,然而js强调类型,函数,需要用编译器来”解释“,拔高了门槛。
如今js.net在2010年停止更新,在.net framework中保持静默发布
说来好笑 M$在es4(ie5时代,即jscript,vbs还可以在ie上运行的时候)上表示强烈反对,转头又做出了这么个缝合怪jscript.net——不完全支持es4,还夹带了些私货,把一门彻底的弱类型语言强行向CIL靠拢,实际上在一开始就给他判了死刑
注释的部分为es4支持但jscript.net不支持
  1. import System;
  2. import System.Windows.Forms;
  3. import System.Drawing;
  4. import System.Collections.Generic;
  5. import System.Collections;
  6. //import * from System;
  7. package Jscript_Net_Test{
  8.         class ES1ToES3{
  9.                 var a="Windows Script Host||IE-Jscript";
  10.                 function A(){
  11.                         var name="Jscript";
  12.                         var WshVersion=5.6;
  13.                         var IE=7;
  14.                         var __null__=null;
  15.                         var __undefined__=undefined;
  16.                         var string=new String('hello');
  17.                         WshVersion=parseFloat(WshVersion);
  18.                         IE=parseInt(IE);
  19.                         MessageBox.Show(('hello'===string).toString());
  20.                         MessageBox.Show(('hello'==string).toString());
  21.                         var obj={
  22.                                 array:[
  23.                                         "a",
  24.                                         1,
  25.                                         new Array(1),
  26.                                         new Function("return 1"),
  27.                                         Math.tan,
  28.                                         new Date(),
  29.                                         function(){
  30.                                                 return true;
  31.                                         }
  32.                                 ]
  33.                         };
  34.                         var ifunc=(function(){
  35.                                 var i=0
  36.                                 return function(){
  37.                                         return i++;
  38.                                 }
  39.                         }())
  40.                         with(Math){
  41.                                 var t=tan(1);
  42.                         }
  43.                 }
  44.         }
  45.         class JscripOnly{
  46.                 var enumobj=new Enumerator([]);
  47.         }
  48.         class ES4{
  49.                 const a="";
  50.                 internal const foo = 5;
  51.                 //const a=""" I'm "Jscript.Net" """;
  52.                 //var b:(Number,String);
  53.                 /*function LIKE(param1:like{x:int,y:int},param2:double):String{
  54.                         return "";
  55.                 }*/
  56.                 interface IMyInterface{
  57.                         function foo():double;
  58.                 }
  59.                 /*class Wrapper<T> {
  60.                         inner: T
  61.                 }*/
  62.                 //generic function addItem(x);
  63.                 function addItem(x: int) {
  64.                 }
  65.                 function addItem(x: Number) {
  66.                 }
  67.                 //const foo = <div class={a}>{"Hello" + a}</div>;
  68.                 public function b(){
  69.                 }
  70.                 private function c(){
  71.                 }
  72.         }
  73.         class Jscript_NetOnly{
  74.                 expando class MyExpandoClass {
  75.                            function dump() {
  76.                       for (var x : String in this)
  77.                          print(x + " = " + this[x]);
  78.                              }
  79.                   }
  80.         }
  81. }
复制代码
你好

当把jscript.net翻译成c# 可以看出实际上是把js.net“解释”,再解释执行(但凡与js天生的灵活性相关的都用类似反射的方法来赋值)
  1. using System;
  2. using System.Windows.Forms;
  3. using Microsoft.JScript;
  4. using Microsoft.JScript.Vsa;
  5. namespace Jscript_Net_Test;
  6. [Serializable]
  7. public class ES1ToES3 : INeedEngine
  8. {
  9.     public object a;
  10.     [NonSerialized]
  11.     private VsaEngine vsa_0020Engine;
  12.     static ES1ToES3()
  13.     {
  14.     }//Error decoding local variables: Signature type sequence must have at least one element.
  15.     private void _002Einit()
  16.     {
  17.         //Error decoding local variables: Signature type sequence must have at least one element.
  18.         a = "Windows Script Host||IE-Jscript";
  19.     }
  20.     public virtual void A()
  21.     {
  22.         //IL_0005: Unknown result type (might be due to invalid IL or missing references)
  23.         //IL_000c: Expected O, but got Unknown
  24.         //IL_0034: Unknown result type (might be due to invalid IL or missing references)
  25.         //IL_003a: Expected O, but got Unknown
  26.         //IL_0076: Unknown result type (might be due to invalid IL or missing references)
  27.         //IL_007c: Expected O, but got Unknown
  28.         //IL_0089: Unknown result type (might be due to invalid IL or missing references)
  29.         //IL_008f: Expected O, but got Unknown
  30.         //IL_0112: Unknown result type (might be due to invalid IL or missing references)
  31.         //IL_0134: Unknown result type (might be due to invalid IL or missing references)
  32.         //IL_01ca: Unknown result type (might be due to invalid IL or missing references)
  33.         //IL_01d0: Expected O, but got Unknown
  34.         //IL_01e7: Unknown result type (might be due to invalid IL or missing references)
  35.         //IL_01f3: Unknown result type (might be due to invalid IL or missing references)
  36.         //IL_020b: Expected O, but got Unknown
  37.         LateBinding val = new LateBinding("tan");
  38.         FunctionObject val2 = FunctionExpression.JScriptFunctionExpression(typeof(ES1ToES3).TypeHandle, "anonymous 0", "A.anonymous 0", new string[0], (JSLocalField[])(object)new JSLocalField[1]
  39.         {
  40.             new JSLocalField("return value", typeof(bool).TypeHandle, 0)
  41.         }, false, false, "function(){\r\n\t\t\t\t\t\treturn true;\r\n\t\t\t\t\t}", ((INeedEngine)this).GetEngine());
  42.         FunctionObject val3 = FunctionExpression.JScriptFunctionExpression(typeof(ES1ToES3).TypeHandle, "anonymous 2", "A.anonymous 2", new string[0], (JSLocalField[])(object)new JSLocalField[2]
  43.         {
  44.             new JSLocalField("i", typeof(double).TypeHandle, 0),
  45.             new JSLocalField("return value", typeof(ScriptFunction).TypeHandle, 1)
  46.         }, true, false, "function(){\r\n\t\t\t\tvar i=0\r\n\t\t\t\treturn function(){\r\n\t\t\t\t\treturn i++;\r\n\t\t\t\t}\r\n\t\t\t}", ((INeedEngine)this).GetEngine());
  47.         string text = "Jscript";
  48.         double num = 5.6;
  49.         double num2 = 7.0;
  50.         object value = DBNull.Value;
  51.         object obj = null;
  52.         StringObject val4 = GlobalObject.String.CreateInstance(new object[1] { "hello" });
  53.         num = GlobalObject.parseFloat((object)num);
  54.         num2 = GlobalObject.parseInt((object)num2, (object)Missing.Value);
  55.         MessageBox.Show(BooleanPrototype.toString((object)(StrictEquality.JScriptStrictEquals((object)"hello", (object)val4) ? true : false)));
  56.         MessageBox.Show(BooleanPrototype.toString((object)(Equality.JScriptEquals((object)"hello", (object)val4) ? true : false)));
  57.         JSObject obj2 = ((INeedEngine)this).GetEngine().GetOriginalObjectConstructor().ConstructObject();
  58.         object[] obj3 = new object[7]
  59.         {
  60.             "a",
  61.             1,
  62.             GlobalObject.Array.CreateInstance(new object[1] { 1 }),
  63.             GlobalObject.Function.CreateInstance(new object[1] { "return 1" }),
  64.             null,
  65.             null,
  66.             null
  67.         };
  68.         val.obj = GlobalObject.Math;
  69.         obj3[4] = val.GetNonMissingValue();
  70.         obj3[5] = GlobalObject.Date.CreateInstance(new object[0]);
  71.         obj3[6] = (object)new Closure(val2);
  72.         obj2.SetMemberValue2("array", (object)Globals.ConstructArrayLiteral(obj3));
  73.         object obj4 = obj2;
  74.         object obj5 = LateBinding.CallValue((object)((IActivationObject)((INeedEngine)this).GetEngine().ScriptObjectStackTop()).GetGlobalScope(), (object)new Closure(val3), new object[0], false, false, ((INeedEngine)this).GetEngine());
  75.         With.JScriptWith((object)GlobalObject.Math, ((INeedEngine)this).GetEngine());
  76.         try
  77.         {
  78.             double num3 = MathObject.tan(1.0);
  79.         }
  80.         finally
  81.         {
  82.             ((INeedEngine)this).GetEngine().PopScriptObject();
  83.         }
  84.     }
  85.     [JSFunction(/*Could not decode attribute arguments.*/)]
  86.     public static bool A_002Eanonymous_00200(object @this, VsaEngine vsa_0020Engine)
  87.     {
  88.         return true;
  89.     }
  90.     [JSFunction(/*Could not decode attribute arguments.*/)]
  91.     public static ScriptFunction A_002Eanonymous_00202(object @this, VsaEngine vsa_0020Engine)
  92.     {
  93.         //IL_0028: Unknown result type (might be due to invalid IL or missing references)
  94.         //IL_002e: Expected O, but got Unknown
  95.         //IL_003b: Unknown result type (might be due to invalid IL or missing references)
  96.         //IL_0041: Expected O, but got Unknown
  97.         //IL_0053: Unknown result type (might be due to invalid IL or missing references)
  98.         //IL_0059: Expected O, but got Unknown
  99.         //IL_0064: Unknown result type (might be due to invalid IL or missing references)
  100.         FunctionObject val = FunctionExpression.JScriptFunctionExpression(typeof(ES1ToES3).TypeHandle, "anonymous 1", "A.anonymous 2.anonymous 1", new string[0], (JSLocalField[])(object)new JSLocalField[2]
  101.         {
  102.             new JSLocalField("return value", typeof(double).TypeHandle, 0),
  103.             new JSLocalField("i", typeof(double).TypeHandle, 1)
  104.         }, false, false, "function(){\r\n\t\t\t\t\treturn i++;\r\n\t\t\t\t}", vsa_0020Engine);
  105.         double num = 0.0;
  106.         ScriptFunction val2 = (ScriptFunction)new Closure(val);
  107.         object[] localVars = ((StackFrame)vsa_0020Engine.ScriptObjectStackTop()).localVars;
  108.         localVars[0] = num;
  109.         localVars[1] = val2;
  110.         return val2;
  111.     }
  112.     [JSFunction(/*Could not decode attribute arguments.*/)]
  113.     public static double A_002Eanonymous_00202_002Eanonymous_00201(object @this, VsaEngine vsa_0020Engine)
  114.     {
  115.         //IL_000c: Unknown result type (might be due to invalid IL or missing references)
  116.         //IL_0045: Unknown result type (might be due to invalid IL or missing references)
  117.         ScriptObject parent = vsa_0020Engine.ScriptObjectStackTop().GetParent();
  118.         double num = Convert.ToNumber(((StackFrame)parent).localVars[0]);
  119.         parent.GetParent().GetParent();
  120.         double num2;
  121.         num = (num2 = num) + 1.0;
  122.         double result = num2;
  123.         ScriptObject parent2 = vsa_0020Engine.ScriptObjectStackTop().GetParent();
  124.         ((StackFrame)parent2).localVars[0] = num;
  125.         parent2.GetParent().GetParent();
  126.         return result;
  127.     }
  128.     public ES1ToES3()
  129.     {
  130.         //Error decoding local variables: Signature type sequence must have at least one element.
  131.         _002Einit();
  132.     }
  133.     private override VsaEngine GetEngine()
  134.     {
  135.         //Error decoding local variables: Signature type sequence must have at least one element.
  136.         if (vsa_0020Engine == null)
  137.         {
  138.             vsa_0020Engine = VsaEngine.CreateEngineWithType(typeof(ES1ToES3).TypeHandle);
  139.         }
  140.         return vsa_0020Engine;
  141.     }
  142.     private override void SetEngine(VsaEngine P_0)
  143.     {
  144.         //Error decoding local variables: Signature type sequence must have at least one element.
  145.         vsa_0020Engine = P_0;
  146.     }
  147. }
复制代码
你好

TOP

对es4完整支持的估计也就actionscript,而且你都说了ms反对es4的,怎么可能会支持,再说了,es4根本就没有被采纳成为标准
jsn能用dotnet类库,用反射就能调winapi和C#dll,功能上,该有的其实都差不多有,只是没有对应的语法

TOP

回复 3# Five66


   如果你说的反射调用winapi是指把c#代码保存到字符串 再用csc.exe编译,再反射载入assembly,再反射调用的话,那也太麻烦了jsn本就可以直接原生调用c#dll
你好

TOP

回复 4# jyswjjgdwtdtj


我所说的用反射调winapi并不是你说的这个,虽然你这个确实也是一种方法
我说是指是动态平台调用,是用反射相关的类对应的方法来创建程序集,然后设置各种东西后调用,这是一种通用的方案,不需要其他语言,只要支持dotnet类库的都能使用(powershell也能用,虽然确实很麻烦)
示例可以看下面的
http://cx20.main.jp/blog/hello/2013/03/07/hello-win32-api-jscript-net-world/

TOP

回复 5# Five66


   那就是用emit了 还没学到
你好

TOP

返回列表