home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 275 / DPCS0111DVD.ISO / Toolkit / Audio-Visual / VirtualDub / Source / VirtualDub-1.9.10-src.7z / src / Sylia / objects.cpp next >
Encoding:
C/C++ Source or Header  |  2009-09-14  |  11.7 KB  |  420 lines

  1. #include <windows.h>
  2. #include <vd2/system/VDString.h>
  3.  
  4. #include "ScriptInterpreter.h"
  5. #include "ScriptValue.h"
  6. #include "ScriptError.h"
  7.  
  8. namespace {
  9. #define FUNC(name) void name(IVDScriptInterpreter *isi, VDScriptValue *argv, int argc)
  10.  
  11.     FUNC(dprint) {
  12.         char lbuf[12];
  13.  
  14.         while(argc--) {
  15.             if (argv->isInt()) {
  16.                 wsprintf(lbuf, "%ld", argv->asInt());
  17.                 OutputDebugString(lbuf);
  18.             } else if (argv->isString()) {
  19.                 OutputDebugString(*argv->asString());
  20.             } else
  21.                 SCRIPT_ERROR(TYPE_INT_REQUIRED);
  22.  
  23.             ++argv;
  24.         }
  25.     }
  26.  
  27.     FUNC(messagebox) {
  28.         MessageBox(NULL, *argv[0].asString(), *argv[1].asString(), MB_OK);
  29.     }
  30.  
  31.     FUNC(IntToString) {
  32.         char buf[32];
  33.  
  34.         if ((unsigned)_snprintf(buf, sizeof buf, "%d", argv[0].asInt()) > 31)
  35.             buf[0] = 0;
  36.  
  37.         argv[0] = isi->DupCString(buf);
  38.     }
  39.  
  40.     FUNC(LongToString) {
  41.         char buf[32];
  42.  
  43.         if ((unsigned)_snprintf(buf, sizeof buf, "%I64d", argv[0].asLong()) > 31)
  44.             buf[0] = 0;
  45.  
  46.         argv[0] = isi->DupCString(buf);
  47.     }
  48.  
  49.     FUNC(DoubleToString) {
  50.         char buf[256];
  51.  
  52.         if ((unsigned)_snprintf(buf, sizeof buf, "%g", argv[0].asDouble()) > 255)
  53.             buf[0] = 0;
  54.  
  55.         argv[0] = isi->DupCString(buf);
  56.     }
  57.  
  58.     FUNC(StringToString) {
  59.         // don't need to do anything
  60.     }
  61.  
  62.     FUNC(Atoi) {
  63.         char *s = *argv[0].asString();
  64.  
  65.         while(*s == ' ')
  66.             ++s;
  67.  
  68.         errno = 0;
  69.         int v = (int)strtol(s, &s, 0);
  70.  
  71.         if (errno && v != 0)
  72.             SCRIPT_ERROR(NUMERIC_OVERFLOW);
  73.  
  74.         while(*s == ' ')
  75.             ++s;
  76.  
  77.         if (*s)
  78.             SCRIPT_ERROR(STRING_NOT_AN_INTEGER_VALUE);
  79.  
  80.         argv[0] = (int)v;
  81.     }
  82.  
  83.     FUNC(Atol) {
  84.         const char *s = *argv[0].asString();
  85.         char dummy;
  86.         sint64 val;
  87.  
  88.         int result = sscanf(s, " %I64d %c", &val, &dummy);
  89.  
  90.         if (result != 1)
  91.             SCRIPT_ERROR(STRING_NOT_AN_INTEGER_VALUE);
  92.  
  93.         argv[0] = val;
  94.     }
  95.  
  96.     FUNC(Atod) {
  97.         char *s = *argv[0].asString();
  98.  
  99.         while(*s == ' ')
  100.             ++s;
  101.  
  102.         errno = 0;
  103.         double v = strtod(s, &s);
  104.  
  105.         if (errno && v != 0)
  106.             SCRIPT_ERROR(NUMERIC_OVERFLOW);
  107.  
  108.         while(*s == ' ')
  109.             ++s;
  110.  
  111.         if (*s)
  112.             SCRIPT_ERROR(STRING_NOT_A_REAL_VALUE);
  113.  
  114.         argv[0] = v;
  115.     }
  116.  
  117.     FUNC(TypeName) {
  118.         switch(argv[0].type) {
  119.             case VDScriptValue::T_VOID:        argv[0] = isi->DupCString("void"); break;
  120.             case VDScriptValue::T_INT:        argv[0] = isi->DupCString("int"); break;
  121.             case VDScriptValue::T_LONG:        argv[0] = isi->DupCString("long"); break;
  122.             case VDScriptValue::T_DOUBLE:    argv[0] = isi->DupCString("double"); break;
  123.             case VDScriptValue::T_STR:        argv[0] = isi->DupCString("string"); break;
  124.             case VDScriptValue::T_OBJECT:    argv[0] = isi->DupCString("object"); break;
  125.             case VDScriptValue::T_FNAME:    argv[0] = isi->DupCString("method"); break;
  126.             case VDScriptValue::T_FUNCTION:    argv[0] = isi->DupCString("function"); break;
  127.             case VDScriptValue::T_VARLV:    argv[0] = isi->DupCString("var"); break;
  128.             default:                        argv[0] = isi->DupCString("unknown"); break;
  129.         }
  130.     }
  131.  
  132.     FUNC(Assert) {
  133.         if (!argv[0].asInt())
  134.             SCRIPT_ERROR(ASSERTION_FAILED);
  135.     }
  136.  
  137.     FUNC(AssertEqual_int) {
  138.         int x = argv[0].asInt();
  139.         int y = argv[1].asInt();
  140.  
  141.         if (x != y)
  142.             SCRIPT_ERROR(ASSERTION_FAILED);
  143.     }
  144.  
  145.     FUNC(AssertEqual_long) {
  146.         sint64 x = argv[0].asLong();
  147.         sint64 y = argv[1].asLong();
  148.  
  149.         if (x != y)
  150.             SCRIPT_ERROR(ASSERTION_FAILED);
  151.     }
  152.  
  153.     FUNC(AssertEqual_double) {
  154.         double x = argv[0].asDouble();
  155.         double y = argv[1].asDouble();
  156.  
  157.         if (x != y)
  158.             SCRIPT_ERROR(ASSERTION_FAILED);
  159.     }
  160.  
  161.     FUNC(TestOverload1) {argv[0] = 1;}
  162.     FUNC(TestOverload2) {argv[0] = 2;}
  163.     FUNC(TestOverload3) {argv[0] = 3;}
  164.     FUNC(TestOverload4) {argv[0] = 4;}
  165.     FUNC(TestOverload5) {argv[0] = 5;}
  166.     FUNC(TestOverload6) {argv[0] = 6;}
  167.     FUNC(TestOverload7) {argv[0] = 7;}
  168.     FUNC(TestOverload8) {argv[0] = 8;}
  169.     FUNC(TestOverload9) {argv[0] = 9;}
  170.     FUNC(TestOverload10) {argv[0] = 10;}
  171.     FUNC(TestOverload11) {argv[0] = 11;}
  172.     FUNC(TestOverload12) {argv[0] = 12;}
  173.     FUNC(TestOverload13) {argv[0] = 13;}
  174.     FUNC(TestOverload14) {argv[0] = 14;}
  175.  
  176.     FUNC(add_int) {    argv[0] = argv[0].asInt() + argv[1].asInt(); }
  177.     FUNC(add_long) { argv[0] = argv[0].asLong() + argv[1].asLong(); }
  178.     FUNC(add_double) { argv[0] = argv[0].asDouble() + argv[1].asDouble(); }
  179.  
  180.     FUNC(add_string) {
  181.         int l1 = strlen(*argv[0].asString());
  182.         int l2 = strlen(*argv[1].asString());
  183.  
  184.         char **pp = isi->AllocTempString(l1+l2);
  185.  
  186.         memcpy(*pp, *argv[0].asString(), l1);
  187.         memcpy(*pp + l1, *argv[1].asString(), l2);
  188.  
  189.         argv[0] = VDScriptValue(pp);
  190.     }
  191.  
  192.     FUNC(add_string_int) {
  193.         char buf[32];
  194.         sprintf(buf, "%d", argv[1].asInt());
  195.         int l1 = strlen(*argv[0].asString());
  196.         int l2 = strlen(buf);
  197.  
  198.         char **pp = isi->AllocTempString(l1+l2);
  199.  
  200.         memcpy(*pp, *argv[0].asString(), l1);
  201.         memcpy(*pp + l1, buf, l2);
  202.  
  203.         argv[0] = VDScriptValue(pp);
  204.     }
  205.  
  206.     FUNC(add_string_long) {
  207.         char buf[32];
  208.         sprintf(buf, "%I64d", argv[1].asLong());
  209.         int l1 = strlen(*argv[0].asString());
  210.         int l2 = strlen(buf);
  211.  
  212.         char **pp = isi->AllocTempString(l1+l2);
  213.  
  214.         memcpy(*pp, *argv[0].asString(), l1);
  215.         memcpy(*pp + l1, buf, l2);
  216.  
  217.         argv[0] = VDScriptValue(pp);
  218.     }
  219.  
  220.     FUNC(add_string_double) {
  221.         VDStringA tmp;
  222.         tmp.sprintf("%s%g", *argv[0].asString(), argv[1].asDouble());
  223.  
  224.         argv[0] = isi->DupCString(tmp.c_str());
  225.     }
  226.  
  227.     FUNC(upos_int) {}
  228.     FUNC(upos_long) {}
  229.     FUNC(upos_double) {}
  230.  
  231.     FUNC(sub_int) { argv[0] = argv[0].asInt() - argv[1].asInt(); }
  232.     FUNC(sub_long) { argv[0] = argv[0].asLong() - argv[1].asLong(); }
  233.     FUNC(sub_double) { argv[0] = argv[0].asDouble() - argv[1].asDouble(); }
  234.  
  235.     FUNC(uneg_int) { argv[0] = -argv[0].asInt(); }
  236.     FUNC(uneg_long) { argv[0] = -argv[0].asLong(); }
  237.     FUNC(uneg_double) { argv[0] = -argv[0].asDouble(); }
  238.  
  239.     FUNC(mul_int) { argv[0] = argv[0].asInt() * argv[1].asInt(); }
  240.     FUNC(mul_long) { argv[0] = argv[0].asLong() * argv[1].asLong(); }
  241.     FUNC(mul_double) { argv[0] = argv[0].asDouble() * argv[1].asDouble(); }
  242.  
  243.     FUNC(div_int) {
  244.         if (!argv[1].asInt())
  245.             SCRIPT_ERROR(DIVIDE_BY_ZERO);
  246.         argv[0] = argv[0].asInt() / argv[1].asInt();
  247.     }
  248.  
  249.     FUNC(div_long) {
  250.         if (!argv[1].asLong())
  251.             SCRIPT_ERROR(DIVIDE_BY_ZERO);
  252.         argv[0] = argv[0].asLong() / argv[1].asLong();
  253.     }
  254.  
  255.     FUNC(div_double) {
  256.         if (argv[1].asDouble() == 0.0)
  257.             SCRIPT_ERROR(DIVIDE_BY_ZERO);
  258.         argv[0] = argv[0].asDouble() / argv[1].asDouble();
  259.     }
  260.  
  261.     FUNC(mod_int) {
  262.         if (!argv[1].asInt())
  263.             SCRIPT_ERROR(DIVIDE_BY_ZERO);
  264.         argv[0] = argv[0].asInt() % argv[1].asInt();
  265.     }
  266.  
  267.     FUNC(mod_long) {
  268.         if (!argv[1].asLong())
  269.             SCRIPT_ERROR(DIVIDE_BY_ZERO);
  270.         argv[0] = argv[0].asLong() % argv[1].asLong();
  271.     }
  272.  
  273.     FUNC(and_int) { argv[0] = argv[0].asInt() & argv[1].asInt(); }
  274.     FUNC(and_long) { argv[0] = argv[0].asLong() & argv[1].asLong(); }
  275.  
  276.     FUNC(or_int) { argv[0] = argv[0].asInt() | argv[1].asInt(); }
  277.     FUNC(or_long) { argv[0] = argv[0].asLong() | argv[1].asLong(); }
  278.  
  279.     FUNC(xor_int) { argv[0] = argv[0].asInt() ^ argv[1].asInt(); }
  280.     FUNC(xor_long) { argv[0] = argv[0].asLong() ^ argv[1].asLong(); }
  281.  
  282.     FUNC(lt_int) { argv[0] = argv[0].asInt() < argv[1].asInt(); }
  283.     FUNC(lt_long) { argv[0] = argv[0].asLong() < argv[1].asLong(); }
  284.     FUNC(lt_double) { argv[0] = argv[0].asDouble() < argv[1].asDouble(); }
  285.  
  286.     FUNC(gt_int) { argv[0] = argv[0].asInt() > argv[1].asInt(); }
  287.     FUNC(gt_long) { argv[0] = argv[0].asLong() > argv[1].asLong(); }
  288.     FUNC(gt_double) { argv[0] = argv[0].asDouble() > argv[1].asDouble(); }
  289.  
  290.     FUNC(le_int) { argv[0] = argv[0].asInt() <= argv[1].asInt(); }
  291.     FUNC(le_long) { argv[0] = argv[0].asLong() <= argv[1].asLong(); }
  292.     FUNC(le_double) { argv[0] = argv[0].asDouble() <= argv[1].asDouble(); }
  293.  
  294.     FUNC(ge_int) { argv[0] = argv[0].asInt() >= argv[1].asInt(); }
  295.     FUNC(ge_long) { argv[0] = argv[0].asLong() >= argv[1].asLong(); }
  296.     FUNC(ge_double) { argv[0] = argv[0].asDouble() >= argv[1].asDouble(); }
  297.  
  298.     FUNC(eq_int) { argv[0] = argv[0].asInt() == argv[1].asInt(); }
  299.     FUNC(eq_long) { argv[0] = argv[0].asLong() == argv[1].asLong(); }
  300.     FUNC(eq_double) { argv[0] = argv[0].asDouble() == argv[1].asDouble(); }
  301.     FUNC(eq_string) { argv[0] = !strcmp(*argv[0].asString(), *argv[1].asString()); }
  302.  
  303.     FUNC(ne_int) { argv[0] = argv[0].asInt() != argv[1].asInt(); }
  304.     FUNC(ne_long) { argv[0] = argv[0].asLong() != argv[1].asLong(); }
  305.     FUNC(ne_double) { argv[0] = argv[0].asDouble() != argv[1].asDouble(); }
  306.     FUNC(ne_string) { argv[0] = !!strcmp(*argv[0].asString(), *argv[1].asString()); }
  307.  
  308.     FUNC(land_int) { argv[0] = argv[0].asInt() && argv[1].asInt(); }
  309.     FUNC(land_long) { argv[0] = argv[0].asLong() && argv[1].asLong(); }
  310.     FUNC(land_double) { argv[0] = argv[0].asDouble() && argv[1].asDouble(); }
  311.  
  312.     FUNC(lor_int) { argv[0] = argv[0].asInt() || argv[1].asInt(); }
  313.     FUNC(lor_long) { argv[0] = argv[0].asLong() || argv[1].asLong(); }
  314.     FUNC(lor_double) { argv[0] = argv[0].asDouble() || argv[1].asDouble(); }
  315.  
  316.     FUNC(unot_int) { argv[0] = !argv[0].asInt(); }
  317.     FUNC(unot_long) { argv[0] = !argv[0].asLong(); }
  318.     FUNC(unot_double) { argv[0] = !argv[0].asDouble(); }
  319.  
  320.     FUNC(uinv_int) { argv[0] = ~argv[0].asInt(); }
  321.     FUNC(uinv_long) { argv[0] = ~argv[0].asLong(); }
  322. }
  323.  
  324. static const VDScriptFunctionDef objFL_Sylia[]={
  325.     { dprint,        "dprint", "0." },
  326.     { messagebox,    "MessageBox", "0ss" },
  327.     { IntToString,        "ToString", "si" },
  328.     { LongToString,        NULL, "sl" },
  329.     { DoubleToString,    NULL, "sd" },
  330.     { StringToString,    NULL, "ss" },
  331.     { Atoi,            "Atoi", "is" },
  332.     { Atol,            "Atol", "ls" },
  333.     { Atod,            "Atod", "ds" },
  334.     { add_int,        "+", "iii" },
  335.     { add_long,        NULL, "lll" },
  336.     { add_double,    NULL, "ddd" },
  337.     { upos_int,        NULL, "ii" },
  338.     { upos_long,    NULL, "ll" },
  339.     { upos_double,    NULL, "dd" },
  340.     { add_string,    NULL, "sss" },
  341.     { add_string_int,    NULL, "ssi" },
  342.     { add_string_long,    NULL, "ssl" },
  343.     { add_string_double, NULL, "ssd" },
  344.     { sub_int,        "-", "iii" },
  345.     { sub_long,        NULL, "lll" },
  346.     { sub_double,    NULL, "ddd" },
  347.     { uneg_int,        NULL, "ii" },
  348.     { uneg_long,    NULL, "ll" },
  349.     { uneg_double,    NULL, "dd" },
  350.     { mul_int,        "*", "iii" },
  351.     { mul_long,        NULL, "lll" },
  352.     { mul_double,    NULL, "ddd" },
  353.     { div_int,        "/", "iii" },
  354.     { div_long,        NULL, "lll" },
  355.     { div_double,    NULL, "ddd" },
  356.     { mod_int,        "%", "iii" },
  357.     { mod_long,        NULL, "lll" },
  358.     { and_int,        "&", "iii" },
  359.     { and_long,        NULL, "lll" },
  360.     { or_int,        "|", "iii" },
  361.     { or_long,        NULL, "lll" },
  362.     { xor_int,        "^", "iii" },
  363.     { xor_long,        NULL, "lll" },
  364.     { lt_int,        "<", "iii" },
  365.     { lt_long,        NULL, "ill" },
  366.     { lt_double,    NULL, "idd" },
  367.     { gt_int,        ">", "iii" },
  368.     { gt_long,        NULL, "ill" },
  369.     { gt_double,    NULL, "idd" },
  370.     { le_int,        "<=", "iii" },
  371.     { le_long,        NULL, "ill" },
  372.     { le_double,    NULL, "idd" },
  373.     { ge_int,        ">=", "iii" },
  374.     { ge_long,        NULL, "ill" },
  375.     { ge_double,    NULL, "idd" },
  376.     { eq_int,        "==", "iii" },
  377.     { eq_long,        NULL, "ill" },
  378.     { eq_double,    NULL, "idd" },
  379.     { eq_string,    NULL, "iss" },
  380.     { ne_int,        "!=", "iii" },
  381.     { ne_long,        NULL, "ill" },
  382.     { ne_double,    NULL, "idd" },
  383.     { ne_string,    NULL, "iss" },
  384.     { land_int,        "&&", "iii" },
  385.     { land_long,    NULL, "ill" },
  386.     { land_double,    NULL, "idd" },
  387.     { lor_int,        "||", "iii" },
  388.     { lor_long,        NULL, "ill" },
  389.     { lor_double,    NULL, "idd" },
  390.     { unot_int,        "!",  "ii" },
  391.     { unot_long,    NULL,  "ll" },
  392.     { uinv_int,        "~", "ii" },
  393.     { uinv_long,    NULL, "ll" },
  394.     { TypeName,        "TypeName", "s." },
  395.     { Assert,        "Assert", "0i" },
  396.     { AssertEqual_int,        "AssertEqual", "0ii" },
  397.     { AssertEqual_long,        "AssertEqual", "0ll" },
  398.     { AssertEqual_double,    "AssertEqual", "0dd" },
  399.     { TestOverload1, "TestOverloading", "i" },
  400.     { TestOverload2, NULL, "ii" },
  401.     { TestOverload3, NULL, "il" },
  402.     { TestOverload4, NULL, "id" },
  403.     { TestOverload5, NULL, "iii" },
  404.     { TestOverload6, NULL, "iil" },
  405.     { TestOverload7, NULL, "iid" },
  406.     { TestOverload8, NULL, "ili" },
  407.     { TestOverload9, NULL, "ill" },
  408.     { TestOverload10, NULL, "ild" },
  409.     { TestOverload11, NULL, "idi" },
  410.     { TestOverload12, NULL, "idl" },
  411.     { TestOverload13, NULL, "idd" },
  412.     { TestOverload14, NULL, "iii." },
  413.     { NULL }
  414. };
  415.  
  416. VDScriptObject obj_Sylia={
  417.     "VDScriptEngine",
  418.     NULL,
  419.     objFL_Sylia
  420. };