BadScript 2
Loading...
Searching...
No Matches
BadInteropImportHandler.cs
Go to the documentation of this file.
6
8
10{
11 private readonly BadFunction m_GetFunction;
14 private readonly BadFunction m_HasFunction;
16
18 {
19 m_HandlerContext = ctx;
20
21 BadObject has = scriptHandler.GetProperty("Has")
22 .Dereference(null);
23
24 if (has is not BadFunction hasF)
25 {
26 throw new BadRuntimeException("Has function not found");
27 }
28
29 m_HasFunction = hasF;
30
31 BadObject get = scriptHandler.GetProperty("Get")
32 .Dereference(null);
33
34 if (get is not BadFunction getF)
35 {
36 throw new BadRuntimeException("Get function not found");
37 }
38
39 m_GetFunction = getF;
40
41 BadObject getHash = scriptHandler.GetProperty("GetHash")
42 .Dereference(null);
43
44 if (getHash is not BadFunction getHashF)
45 {
46 throw new BadRuntimeException("GetHash function not found");
47 }
48
49 m_GetHashFunction = getHashF;
50
51 BadObject isTransient = scriptHandler.GetProperty("IsTransient")
52 .Dereference(null);
53
54 if (isTransient is not BadFunction isTransientF)
55 {
56 throw new BadRuntimeException("IsTransient function not found");
57 }
58
59 m_IsTransientFunction = isTransientF;
60 }
61
62 private BadObject RunFunction(BadFunction func, params BadObject[] args)
63 {
64 IEnumerable<BadObject> e = func.Invoke(args, m_HandlerContext);
65 BadObject? result = BadObject.Null;
66
67 foreach (BadObject o in e)
68 {
69 result = o;
70 }
71
72 return result.Dereference(null);
73 }
74
75 public override bool IsTransient()
76 {
78
79 if (result is not IBadBoolean b)
80 {
81 throw new BadRuntimeException("IsTransient function did not return a boolean");
82 }
83
84 return b.Value;
85 }
86
87 public override bool Has(string path)
88 {
89 BadObject result = RunFunction(m_HasFunction, path);
90
91 if (result is not IBadBoolean b)
92 {
93 throw new BadRuntimeException("Has function did not return a boolean");
94 }
95
96 return b.Value;
97 }
98
99 public override string GetHash(string path)
100 {
102
103 if (result is not IBadString s)
104 {
105 throw new BadRuntimeException("GetHash function did not return a string");
106 }
107
108 return s.Value;
109 }
110
111 public override IEnumerable<BadObject> Get(string path)
112 {
113 BadObject? result = BadObject.Null;
114
115 foreach (BadObject o in m_GetFunction.Invoke(new BadObject[] { path },
117 ))
118 {
119 result = o;
120
121 yield return o;
122 }
123
124 yield return result.Dereference(null);
125 }
126}
The Execution Context. Every execution of a script needs a context the script is running in....
static BadExecutionContext Create(BadInteropExtensionProvider provider)
Creates a new Execution Context with an empty scope.
Defines the shape of the import handler for the module importer.
BadObject RunFunction(BadFunction func, params BadObject[] args)
BadInteropImportHandler(BadExecutionContext ctx, BadObject scriptHandler)
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
virtual BadObjectReference GetProperty(string propName, BadScope? caller=null)
Returns a Reference to the Property with the given Name.
Definition BadObject.cs:141
static readonly BadObject Null
The Null Value for the BadScript Language.
Definition BadObject.cs:28
Implements a function that can be called from the script.
IEnumerable< BadObject > Invoke(BadObject[] args, BadExecutionContext caller)
Invokes the function with the specified arguments.
Implements the Interface for Native Boolean.
Definition IBadBoolean.cs:7
Implements the Interface for Native Strings.
Definition IBadString.cs:7
Contains the Error Objects for the BadScript2 Language.
Contains the Interop Abstractions and Implementations for the BadScript2 Language.
Contains Runtime Function Objects.
Contains the Native Runtime Objects.
Definition BadBoolean.cs:6
Contains the Runtime Objects.
Definition BadArray.cs:10