1 /* Digital Mars DMDScript source code.
2  * Copyright (c) 2000-2002 by Chromium Communications
3  * D version Copyright (c) 2004-2010 by Digital Mars
4  * Distributed under the Boost Software License, Version 1.0.
5  * (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6  * written by Walter Bright
7  * http://www.digitalmars.com
8  *
9  * D2 port by Dmitry Olshansky 
10  *
11  * DMDScript is implemented in the D Programming Language,
12  * http://www.digitalmars.com/d/
13  *
14  * For a C++ implementation of DMDScript, including COM support, see
15  * http://www.digitalmars.com/dscript/cppscript.html
16  */
17 
18 
19 module dmdscript.dnative;
20 
21 import dmdscript.script;
22 import dmdscript.dobject;
23 import dmdscript.dfunction;
24 import dmdscript.value;
25 
26 /******************* DnativeFunction ****************************/
27 
28 alias void *function(Dobject pthis, CallContext *cc, Dobject othis, Value* ret, Value[] arglist) PCall;
29 
30 struct NativeFunctionData
31 {
32     d_string string;
33     PCall     pcall;
34     d_uint32  length;
35 }
36 
37 class DnativeFunction : Dfunction
38 {
39     PCall pcall;
40 
41     this(PCall func, d_string name, d_uint32 length)
42     {
43         super(length);
44         this.name = name;
45         pcall = func;
46     }
47 
48     this(PCall func, d_string name, d_uint32 length, Dobject o)
49     {
50         super(length, o);
51         this.name = name;
52         pcall = func;
53     }
54 
55     override void* Call(CallContext *cc, Dobject othis, Value* ret, Value[] arglist)
56     {
57         return (*pcall)(this, cc, othis, ret, arglist);
58     }
59 
60     /*********************************
61      * Initalize table of native functions designed
62      * to go in as properties of o.
63      */
64 
65     static void initialize(Dobject o, NativeFunctionData[] nfd, uint attributes)
66     {
67         Dobject f = Dfunction.getPrototype();
68 
69         for(size_t i = 0; i < nfd.length; i++)
70         {
71             NativeFunctionData* n = &nfd[i];
72 
73             o.Put(n..string,
74                   new DnativeFunction(n.pcall, n..string, n.length, f),
75                   attributes);
76         }
77     }
78 }