first commit

This commit is contained in:
2026-06-22 11:49:35 +02:00
commit d805f2ba86
619 changed files with 126873 additions and 0 deletions

View File

@ -0,0 +1,42 @@
/* -----------------------------------------------------------------------------
GSFramework
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
----------------------------------------------------------------------------- */
#ifndef __SCRIPTBINDINGINTERFACE__
#define __SCRIPTBINDINGINTERFACE__
#include "plugin/plugin_manager.h"
namespace GS {
namespace Script {
class IVM;
/// Interface to provide additional script binding to a VM.
struct IScriptBinding
{
/// Return the plugin class.
static const char *GetPluginClass() { return "ScriptBinding"; }
/*!
@short Return the plugin version.
@note This version number must be increased whenever a modification
is made to the base interface in order to prevent an incompatible
plugin from being loaded.
*/
static uint GetPluginVersion() { return 1; }
/// Register the new script binding code into the VM.
virtual void RegisterBinding(IVM &) = 0;
};
typedef PluginManager <IScriptBinding> BindingPluginManager;
} // Script
} // GS
#endif // __SCRIPTBINDINGINTERFACE__

View File

@ -0,0 +1,169 @@
/* -----------------------------------------------------------------------------
GSFramework
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
----------------------------------------------------------------------------- */
#ifndef __SCRIPT_DEBUGGER__
#define __SCRIPT_DEBUGGER__
#include "script/script_vm.h"
#include "script/script_engine_types.h"
namespace GS {
namespace NML { class Tag; }
namespace Script {
/// Debugger variable.
struct DebuggerVariable
{
String id;
uint type;
String type_string;
bool referenced;
bool modified;
AutoList <DebuggerVariable *> member_list;
union
{
bool v_bool;
int v_int;
float v_float;
};
String v_string;
String value;
DebuggerVariable()
{
type = 0;
referenced = false;
}
};
/// Source breakpoint.
struct SourceBreakpoint
{
String source;
int line;
SourceBreakpoint(const char *s, int l)
{ source = s; line = l; }
};
/*
@short Script debugger interface.
@author Emmanuel Julien (ejulien@nworks.fr)
*/
class IDebugger
{
protected:
bool is_suspended,
is_stepping;
int debug_stack_frame,
step_to_callstack_depth;
DebuggerVariable *locals_root;
AutoList <DebuggerVariable *> local_var_tree;
AutoList <SourceBreakpoint *> source_breakpoint_list;
/// Mark all variables in tree as non-referenced.
void DereferenceVarTree(const List <DebuggerVariable *> &tree);
public:
/*!
@name Interface
@{
*/
/// Format variable parameter.
virtual String FormatParameter(DebuggerVariable *variable) = 0;
/// Format safe ptr parameter.
virtual String FormatUserObjectParameter(CObjectType type) = 0;
/// Convert a debugger variable to a meta string.
virtual void VariableToMetatagString(DebuggerVariable *, String &) = 0;
/// Get call stack depth.
virtual int GetCallstackDepth() = 0;
/// Get current script execution call frame index.
virtual int GetStackFrameIndex() = 0;
/// Refresh stack locals list.
virtual void RefreshStackFrameLocalsCache() = 0;
/// Get source/line info for the current debug stack frame.
virtual void GetStackFrameSource(const char *&, int &) = 0;
/// @}
/*!
@name Breakpoint.
@{
*/
/// Check for suspend on breakpoint.
void CheckSuspendOnBreakpoint(const char *source, int line);
/// Check for suspend on step.
void CheckSuspendOnStep();
/// Find a source breakpoint.
SourceBreakpoint *FindSourceBreakpoint(const char *source, int line);
/// Add a new source breakpoint.
SourceBreakpoint *AddSourceBreakpoint(const char *source, int line);
/// Remove a source breakpoint.
bool RemoveSourceBreakpoint(SourceBreakpoint *breakpoint);
/// Set breakpoints from a tag.
bool SetBreakpoints(const NML::Tag &tag);
/// @}
/*!
@name Execution control.
@{
*/
/// Suspend execution.
void Suspend();
/// Resume execution.
void Resume();
/// Step to the next instruction.
void Step();
/// Step into the next call.
void StepInto();
/// Step out of the current call.
void StepOut();
/// Return whether the current scene script execution is suspended or not.
bool IsSuspended() const { return is_suspended; }
/// Is the debugger stepping.
bool IsStepping() const { return is_stepping; }
/// @}
/*!
@name Interface control.
@{
*/
/// Set the call stack debug depth.
void SetDebugStackFrame(int depth);
/// Get call stack debug depth.
int GetDebugStackFrame() const { return debug_stack_frame; }
/// Get stack locals as a meta string.
String GetStackFrameLocals();
void Reset();
/// @}
IDebugger();
virtual ~IDebugger();
};
} // Script
} // GS
#endif // __SCRIPT_DEBUGGER__

View File

@ -0,0 +1,99 @@
/* -----------------------------------------------------------------------------
GSFramework
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
----------------------------------------------------------------------------- */
#ifndef __SCRIPTENGINETYPES__
#define __SCRIPTENGINETYPES__
namespace GS {
namespace Script {
/// Native C object types that the VM can interact with.
enum CObjectType
{
typetag_Undefined = 0,
typetag_Deleted, // The native object was deleted.
typetag_Engine,
typetag_ScriptVM,
typetag_ResourceSet,
typetag_Project,
typetag_Renderer,
typetag_Raytracer,
typetag_Mixer,
typetag_Scene3d,
typetag_Clock,
typetag_Font,
typetag_RasterFont,
typetag_Scene2d,
typetag_UICursor,
typetag_UIItem,
typetag_UISprite,
typetag_Window,
typetag_Widget,
typetag_SizerWidget,
typetag_ContainerWidget,
typetag_SpacerWidget,
typetag_CanvasWidget,
typetag_TextWidget,
typetag_BitmapWidget,
typetag_CheckWidget,
typetag_Picture,
typetag_Group,
typetag_Item,
typetag_Camera,
typetag_Object,
typetag_Light,
typetag_Instance,
typetag_Emitter,
typetag_ParticleModel,
typetag_Motion,
typetag_Trigger,
typetag_Path,
typetag_Sound,
typetag_Shader,
typetag_Texture,
typetag_Geometry,
typetag_GeometryTemplate,
typetag_Material,
typetag_MaterialShader,
typetag_ColShape,
typetag_Constraint,
typetag_Metafile,
typetag_Metatag,
typetag_FileHandle,
typetag_InputDevice,
typetag_EditorPlugin,
typetag_ProjectScene,
typetag_ProjectLayer,
typetag_AutomationSource,
typetag_AutomationSourceGroup,
typetag_ResourceFactories,
typetag_PeerController,
typetag_Peer,
typetag_WebSocketManager,
typetag_End
};
const char *CObjectTypeToString(CObjectType type);
} // Script
} // GS
#endif // __SCRIPTENGINETYPES__

View File

@ -0,0 +1,65 @@
/* -----------------------------------------------------------------------------
GSFramework
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
----------------------------------------------------------------------------- */
#ifndef __NSCRIPTOBJECT__
#define __NSCRIPTOBJECT__
#include "script/script_vm.h"
namespace GS {
namespace Script {
/*
@short Abstract script object.
@author Emmanuel Julien (ejulien@gsworks.fr)
*/
class Object
{
protected:
IVM &vm;
public:
IVM &GetVM() const { return vm; }
/// Perform a set operation on this object.
bool Set(const char *name, const Variant &p) { return vm.Set(name, p, this); }
/// Perform a get operation on this object.
bool Get(const char *name, Variant &p) { return vm.Get(name, p, this); }
/// Append a variant to this object.
bool Append(const Variant &p) { return vm.Append(p, this); }
/// Setup a function call on this object.
bool SetupFunctionCall(const char *func, const Object *function_object = 0)
{ return vm.SetupFunctionCall(func, function_object, this); }
/// Push null function call argument.
bool PushFunctionCallNullArgument()
{ return vm.PushNullArgument(); }
/// Push function call argument.
bool PushFunctionCallArgument(const Variant &arg)
{ return vm.PushArgument(arg); }
/// Execute a function call on this object, return value as a variant.
bool DoFunctionCall(Variant *return_value = 0)
{ return vm.DoFunctionCall(return_value); }
Object &operator= (const Object &b)
{
vm = b.GetVM();
return *this;
}
Object(IVM &v) : vm(v) {}
virtual ~Object() {}
};
} // Script
} // GS
#endif // __NSCRIPTOBJECT__

View File

@ -0,0 +1,100 @@
/* -----------------------------------------------------------------------------
GSFramework
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
----------------------------------------------------------------------------- */
#ifndef __NSCRIPTPROFILER__
#define __NSCRIPTPROFILER__
#include "nstring/nstring.h"
#include "container/nlist.h"
namespace GS {
namespace Script {
/*
@short Script VM profiler.
@author Emmanuel Julien (ejulien@gsworks.fr)
*/
class Profiler
{
public:
struct FunctionProfile
{
String func, ///< The function name.
source; ///< The source name.
int line; ///< Function start line.
int hit_count, ///< Number of total call to this function.
total_clock, ///< Total execution clock.
self_clock;
List <FunctionProfile *> callee_function;
FunctionProfile *caller_function;
/// Hit function profile.
void Hit() { hit_count++; }
/// Update function profile.
void Update(int dt_clock, int /*line*/) { self_clock += dt_clock; }
FunctionProfile(const char *sourcename, const char *funcname, int _line)
{
source = sourcename;
func = funcname;
line = _line;
caller_function = 0;
hit_count = 0;
total_clock = 0;
self_clock = 0;
}
};
protected:
int profiler_clock,
total_clock;
bool profiling;
FunctionProfile *current_function_profile;
public:
String id; ///< Profiler session id.
/// Profiled function stack.
List <FunctionProfile *> function_map; ///< The profiled function map (all profiles from the call graph).
List <FunctionProfile *> function_list; ///< The profiled function list (each function figures only once in this list).
/// Get child total clock.
int GetChildTotalClock(FunctionProfile *profile);
/// Get the last profile session total running clock.
int GetTotalClock() const { return total_clock; }
/// Is a profiler session opened.
bool IsProfiling() const { return profiling; }
/// Request a function profile.
FunctionProfile *GetFunctionProfile(const char *source, const char *func, int line, FunctionProfile *caller);
/// Invalidate the current function profile.
void InvalidateCurrentFunctionProfile() { current_function_profile = 0; }
/// Start a profiler session.
void Start(const char *session_id);
/// Update profiler.
void Update(int type, FunctionProfile *profile);
/// End a profiler session.
void End();
Profiler();
};
} // Script
} // GS
#endif // __NSCRIPTPROFILER__

View File

@ -0,0 +1,88 @@
/* -----------------------------------------------------------------------------
GSFramework
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
----------------------------------------------------------------------------- */
#ifndef __NSCRIPTUNIT__
#define __NSCRIPTUNIT__
#include "script/script_vm.h"
#include "data/nvariant.h"
namespace GS {
namespace NML { class Tag; }
namespace Script {
/*!
@short Script unit.
@author Emmanuel Julien (ejulien@gsworks.fr)
*/
class Unit
{
protected:
Object *self;
uint iface_type; ///< Interface object type.
void *iface_object; ///< Interface object.
public:
sVM vm;
const Object *Self() const { return self; }
List <GS::Variant *> parm_list;
String script_file, ///< Script file to process.
script_class; ///< Class to instantiate upon setup.
/// Set unit interface object.
void SetInterfaceObject(void *p, uint type)
{
iface_object = p;
iface_type = type;
}
/// Get unit interface object.
void GetInterfaceObject(void *&p, uint &type)
{
p = iface_object;
type = iface_type;
}
/*!
@name Runtime.
@{
*/
/// Open component.
virtual bool Open();
/// Close component.
virtual void Close();
/// Is unit open.
bool IsOpen() const { return asbool(self); }
/// Setup a function call in the VM.
virtual bool SetupFunctionCall(const char *func, const Object *func_object = 0);
/// Push user object function call argument.
virtual bool PushUserObjectFunctionCallArgument(void *, uint, bool managed = false);
/// Push function call argument.
virtual bool PushFunctionCallArgument(const Variant &);
/// Execute a function call in the VM, return value as a variant.
virtual bool DoFunctionCall(Variant *return_value = 0);
/// @}
bool FromMetaTag(NML::Tag &);
NML::Tag *AsMetaTag() const;
Unit(IVM *);
virtual ~Unit();
};
} // Script
} // GS
#endif // __NSCRIPTUNIT__

View File

@ -0,0 +1,69 @@
/* -----------------------------------------------------------------------------
GSFramework
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
----------------------------------------------------------------------------- */
#ifndef __NSCRIPTVARIANT__
#define __NSCRIPTVARIANT__
#include "data/nvariant.h"
#include "script/script_vm.h"
namespace GS {
namespace Script {
class Object;
/// Script variant.
struct Variant
{
enum Type
{
Type_None = 0,
Type_Variant,
Type_ScriptObject,
Type_UserObject
};
Type type;
GS::Variant variant;
bool object_owner;
Object *object;
void *ptr;
uint typetag;
bool operator == (const Variant &) const;
bool operator != (const Variant &) const;
void Set();
void Set(bool v);
void Set(int v);
void Set(uint v);
void Set(float v);
void Set(const char *v);
void Set(const GS::Variant &v);
void Set(Object *object, bool object_owner = false);
void Set(const void *p, size_t size);
void Set(void *ptr, uint typetag);
Variant();
Variant(bool v);
Variant(int v);
Variant(uint v);
Variant(float v);
Variant(const char *v);
Variant(const GS::Variant &v);
Variant(Object *object, bool object_owner = false);
Variant(const void *p, size_t size);
Variant(void *ptr, uint typetag);
~Variant();
};
} // Script
} // GS
#endif // __NSCRIPTVARIANT__

View File

@ -0,0 +1,145 @@
/* -----------------------------------------------------------------------------
GSFramework
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
----------------------------------------------------------------------------- */
#ifndef __SCRIPTVMINTERFACE__
#define __SCRIPTVMINTERFACE__
#include "script/script_binding_interface.h"
#include "http/http_interface.h"
#include "network/network_interface.h"
#include "memory/nshared_ptr.h"
namespace GS {
namespace Script {
struct Variant;
class Object;
/*
@short Script virtual machine abstract interface.
@author Emmanuel Julien (ejulien@gsworks.fr)
*/
class IVM : public SharedObject
{
public:
enum State
{
StateOk = 0,
StateExceptionThrown, ///< Crashed and open for inspection.
StateDead ///< Crashed with no hope for inspection.
};
struct IDebug
{
/// Handle a runtime debug step.
virtual void OnStep(char type, const char *source, int line, const char *funcname) = 0;
/// Handle a VM kill event.
virtual void OnFatalError(const char *reason) = 0;
/// Handle a compiler error.
virtual void OnCompilerError(const char *error, const char *source, int line) = 0;
/// Handle a runtime error.
virtual void OnRuntimeException(const char *error) = 0;
virtual ~IDebug() {}
};
protected:
State state;
AutoPtr <IDebug> debug_interface;
public:
BindingPluginManager binding_plugins;
AutoPtr <Network::INetwork> peer_net;
AutoPtr <HTTP::IHTTP> http;
/*!
@name Debugging interface.
@{
*/
struct CallStackEntry
{
String source, function;
int line;
CallStackEntry(const char *s, const char *f, int l) : source(s), function(f), line(l) {}
};
/// Get VM call stack.
virtual void GetCallStack(AutoList <CallStackEntry *> &callstack) { callstack.Clear(); }
/// @}
/// Set VM state.
void SetState(State s) { state = s; }
/// Get VM state.
State GetState() const { return state; }
/// Get virtual machine name.
virtual const char *GetName() const = 0;
/// Set VM event hook table.
virtual void SetDebugInterface(IDebug *, bool enable_debugging = false);
/// Get the VM event hook table.
IDebug *GetEventHandler() const { return debug_interface; }
/// Get a VM object.
virtual Object *GetObjectFromName(const char *name, const Object *context = 0) = 0;
/// Compile a script.
virtual bool Compile(const char *source, uint size, const Object *context = 0, const char *sourcename = 0) = 0;
/// Compile a script from file.
virtual bool CompileFile(const char *uri, const Object *context = 0);
/// Create a script table.
virtual Object *CreateTable() = 0;
/// Set a VM variable.
virtual bool Set(const char *name, const Variant &prop, const Object *context = 0) = 0;
/// Get a VM variable.
virtual bool Get(const char *name, Variant &prop, const Object *context = 0) = 0;
/// Create a script array.
virtual Object *CreateArray() = 0;
/// Append a value to an array.
virtual bool Append(const Variant &, const Object *context = 0) = 0;
/// Setup a function call in the VM.
virtual bool SetupFunctionCall(const char *func, const Object *function_object = 0, const Object *search_context = 0) = 0;
/// Set function call context.
virtual bool SetFunctionCallContext(const Variant &) = 0;
/// Push function call null argument.
virtual bool PushNullArgument() = 0;
/// Push function call argument.
virtual bool PushArgument(const Variant &) = 0;
/// Execute a function call in the VM, return value as a variant.
virtual bool DoFunctionCall(Variant *return_value = 0) = 0;
/// Invalidate all references to a user object.
virtual int InvalidateNativeReference(void *) = 0;
/// Is the VM open.
virtual bool IsOpen() const = 0;
/// Open the script VM.
virtual bool Open() = 0;
/// Crash the VM.
virtual void Kill(const char *reason);
/// Close the script VM.
virtual void Close() = 0;
IVM();
virtual ~IVM();
};
typedef SharedPtr <IVM> sVM;
} // Script
} // GS
#endif // __SCRIPTVMINTERFACE__

View File

@ -0,0 +1,58 @@
/* -----------------------------------------------------------------------------
GSFramework
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
----------------------------------------------------------------------------- */
#ifndef __SCRIPT_VM_DEBUG_PROFILE_EVENT_HANDLER__
#define __SCRIPT_VM_DEBUG_PROFILE_EVENT_HANDLER__
#include "script/script_vm.h"
#include "script/script_debugger.h"
#include "memory/nauto_ptr.h"
namespace GS {
namespace Script {
/*!
@short Debugger/profiling VM event handler.
This handler provides basic functionality such as VM stepping and profiling.
This class should be further derived to implement complete control over the VM.
@author Emmanuel Julien (ejulien@owloh.com)
*/
class IDebuggerProfiler : public IVM::IDebug
{
static String ConvertCallStackToMetaString(const AutoList <IVM::CallStackEntry *> &, int stack_frame_index);
protected:
sVM vm;
public:
AutoPtr <IDebugger> debugger;
String GetCallstack();
String GetDebugStackFrameLocals();
void Kill();
// Debugger interface.
virtual void OnSuspendExecution(const char *source, int line) = 0;
virtual bool OnUpdateSuspendedExecution() = 0;
// VM interface implementation.
void OnStep(char type, const char *source, int line, const char *func);
IDebuggerProfiler(IVM *vm, IDebugger *debugger);
};
} // Script
} // GS
#endif // __SCRIPT_VM_DEBUG_PROFILE_EVENT_HANDLER__

View File

@ -0,0 +1,62 @@
/* -----------------------------------------------------------------------------
GSFramework
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
----------------------------------------------------------------------------- */
#ifndef __NSCRIPTEDOBJECT__
#define __NSCRIPTEDOBJECT__
#include "script/script_vm.h"
#include "container/nlist.h"
namespace GS {
namespace NML { class Tag; }
namespace Script {
class Unit;
/*
@short Scripted object base class.
@author Emmanuel Julien (ejulien@gsworks.fr)
*/
class ScriptedObject
{
List <Unit *> unit_list;
protected:
sVM vm;
public:
/// Unit factory.
virtual Unit *NewUnit() const = 0;
IVM *GetVM() const { return vm; }
Unit *AddUnit(Unit *);
bool RemoveUnit(Unit *);
void RemoveAllUnit();
Unit *GetUnit(const char *script_path, const char *script_class) const;
const List <Unit *> &GetUnitList() const { return unit_list; }
/*!
@name Serialization.
@{
*/
bool FromMetaTag(NML::Tag &);
NML::Tag *AsMetaTag() const;
/// @}
ScriptedObject(IVM *v) : vm(v) {}
virtual ~ScriptedObject();
};
} // Script
} // GS
#endif // __NSCRIPTEDOBJECT__