Added support for ISourcePawnEngine natives.
2 * Copyright (C) 2004-2012 David Anderson
4 * This file is part of SourcePawn.
6 * SourcePawn is free software: you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License as published by the Free
8 * Software Foundation, either version 3 of the License, or (at your option)
11 * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along with
16 * SourcePawn. If not, see http://www.gnu.org/licenses/.
18 #ifndef _include_sourcepawn3_api_h_
19 #define _include_sourcepawn3_api_h_
23 #include "../include/keima_util.h"
39 * A Handle is a safe pointer that holds an object reference, such that it
40 * can be scanned and potentially moved by the garbage collector.
46 template <typename S> void typeCheck() {
61 template <typename S> Handle(Handle<S> other)
62 : obj_(reinterpret_cast<S *>(*other))
67 explicit Handle(T *obj)
72 T *operator ->() const {
75 T *operator *() const {
78 operator bool () const {
87 * A local variable that roots an object. Locals are owned by the nearest
88 * HandleScope, and as soon as that HandleScope dies, all its Locals are
89 * no longer available.
92 class Local : public Handle<T>
100 template <typename S> Local(Local<S> other)
101 : Handle<T>(reinterpret_cast<T *>(*other))
106 template <typename S> Local(S *other)
113 * A non-local variable that outlives HandleScopes. A Root is reference-
114 * counted. When its reference count reaches 0, the Root is removed.
116 template <typename T>
117 class Root : public Handle<T>
125 Root<T>(const Root<T> &other)
126 : Handle<T>(reinterpret_cast<T *>(*other))
131 explicit Root(ke::HeapRoot *obj)
132 : Handle<T>(reinterpret_cast<T *>(obj))
137 static inline Root<T> New(Handle<T> other);
143 Root<T> &operator =(const Root<T> &other) {
156 * A HandleScope is where Local roots are allcoated. When the HandleScope
157 * is destroyed, all Handles created under it are also destroyed. HandleScopes
158 * may only be allocated on the stack, in LIFO order.
164 ke::Object **position_;
167 HandleScope(ke::Zone *zone, ke::Object **position, HandleScope *prev)
178 static ke::Object **CreateHandle(ke::Object *obj);
181 // Disallow silly stuff.
182 HandleScope(const HandleScope &);
183 void operator =(const HandleScope &);
184 void *operator new (size_t size);
185 void operator delete(void *, size_t);
190 // A string is an immutable UTF8 buffer.
191 class KE_EXPORT String
194 // Returns the length of the string, in bytes.
197 // Returns the character buffer of the string, or NULL on failure. If the
198 // string is interned, chars() is guaranteed to succeed.
202 // A function is an executable unit of code.
203 class KE_EXPORT Function
206 // Returns the name of a function, which may be NULL. If not NULL, the
207 // string is guaranteed to be interned.
208 Local<String> name();
211 // A native is an ISourcePawnEngine-compatible function provided by C++.
212 class KE_EXPORT Native
215 // Returns the name of a native, which is never NULL. The string is
216 // guaranteed to be interned.
217 Local<String> name();
219 // Returns the function pointer of the native.
222 // Sets the function pointer of the native.
223 void bind(void *function);
226 // A module represents a single compiled file, and contains functions and
227 // global variables for that file (as well as exported types).
228 class KE_EXPORT Module
231 // Returns publics that are based on forwards.
232 unsigned numPublics();
233 Local<Function> getPublic(unsigned index);
236 unsigned numNatives();
237 Local<Native> getNative(unsigned index);
239 // Sets the native identity pointer, which is passed as the first argument
241 void setNativeIdentity(void *ptr);
248 * Initializes the Keima engine. This must be called exactly once before any
249 * other Keima functions are called. After this, it is safe to use Keima
250 * on the calling thread.
252 static bool Initialize();
255 * Shuts down the Keima engine and frees data structures on all threads.
257 static void Shutdown();
260 * Searches the call stack and returns true if the given module has any
261 * active code running. This is an O(n) algorithm.
263 static bool IsInCallStack(Handle<Module> module);
266 * Reclaim as much memory as possible. This pauses the calling thread
267 * until garbage collection completes.
269 static void CollectGarbage();
272 * Clear any pending exception.
274 static void ClearPendingException();
277 * Type checks a function against given parameters.
279 static bool CheckInvokeTypes(Handle<Function> fun, const Parameter *params, unsigned nparams);
282 * Executes a function with the given parameters. This interface is
283 * intended for compatibility with SourceMod's ISourcePawnEngine2 and
284 * should not be used otherwise, since it is intended to support only
285 * a small subset of the core language.
287 static bool Invoke(Handle<Function> fun, const Parameter *params, unsigned nparams, int *result);
291 * Suppresses exceptions. This is useful for calling API functions. When this
292 * in scope, errors will be reported, but the exception will be cleared after.
294 class AutoClearExceptions
297 ~AutoClearExceptions()
299 Keima::ClearPendingException();
303 // Argument-passing interface for Invoke().
326 Parameter(int *i, bool writeback)
328 writeback_(writeback)
339 Parameter(float *f, bool writeback)
341 writeback_(writeback)
346 Parameter(Type type, void *buffer, size_t length, bool writeback)
348 writeback_(writeback),
351 assert(type == Array || type == CharArray);
360 assert(type() == Int);
364 assert(type() == Float);
368 assert(type() == IntRef);
372 assert(type() == FloatRef);
375 bool writeback() const {
376 assert(type() >= IntRef && type() <= CharArray);
379 size_t length() const {
382 void *buffer() const {
383 assert(type() == Array || type() == CharArray);
402 * Compiles a file and returns a Module.
404 extern Local<Module> Compile(const char *path);
408 template <typename T> friend class Root;
411 static ke::HeapRoot *NewHeapRoot(ke::Object **objp);
412 static void IncRef(ke::HeapRoot *root);
413 static void DecRef(ke::HeapRoot *root);
416 template <typename T> Root<T>
417 Root<T>::New(Handle<T> other)
421 ke::Object **ptr = reinterpret_cast<ke::Object **>(*other);
422 return Root<T>(Internals::NewHeapRoot(ptr));
425 template <typename T> void
430 ke::HeapRoot *ptr = reinterpret_cast<ke::HeapRoot *>(**this);
431 Internals::IncRef(ptr);
434 template <typename T> void
439 ke::HeapRoot *ptr = reinterpret_cast<ke::HeapRoot *>(**this);
440 Internals::DecRef(ptr);
445 #endif // _include_sourcepawn3_api_h_