diff --git a/docs/unity/bindings.md b/docs/unity/bindings.md index 87ca4b91..bc6b045e 100644 --- a/docs/unity/bindings.md +++ b/docs/unity/bindings.md @@ -50,7 +50,7 @@ public class PyVector2Type: PyTypeObject{ public override System.Type type => typeof(Vector2); [PythonBinding] - public static object __add__(VM vm, Vector2 self, object other){ + public object __add__(Vector2 self, object other){ // If the other object is not a Vector2, return NotImplemented if(!(other is Vector2)) return VM.NotImplemented; // Otherwise, return the result of addition @@ -59,7 +59,7 @@ public class PyVector2Type: PyTypeObject{ } ``` -This is easy to understand, right? +This is easy to understand. Let's see another example, `__mul__`, it is used to implement the `*` operator in Python. `Vector2` object in C# can be multiplied with a `float` object in Python. The following code shows this usage. @@ -82,20 +82,19 @@ public class PyVector2Type: PyTypeObject{ // ... [PythonBinding] - public static object __mul__(VM vm, Vector2 self, object other){ + public object __mul__(Vector2 self, object other){ if(!(other is float)) return VM.NotImplemented; return self * (float)other; } [PythonBinding] - public static object __rmul__(VM vm, Vector2 self, object other){ + public object __rmul__(Vector2 self, object other){ if(!(other is float)) return VM.NotImplemented; return self * (float)other; } } ``` - Finally, let's implement the constructor of `Vector2`. `__new__` magic method must be defined. @@ -105,7 +104,7 @@ public class PyVector2Type: PyTypeObject{ public override System.Type type => typeof(Vector2); [PythonBinding] - public static object __new__(VM vm, PyTypeObject cls, params object[] args){ + public object __new__(PyTypeObject cls, params object[] args){ if(args.Length == 0) return new Vector2(); if(args.Length == 2){ float x = vm.PyCast(args[0]); @@ -137,21 +136,20 @@ public class PyVector2Type: PyTypeObject{ public override System.Type type => typeof(Vector2); [PythonBinding(BindingType.Getter)] - public static object x(VM vm, Vector2 self) => self.x; + public object x(Vector2 self) => self.x; [PythonBinding(BindingType.Setter)] - public static void x(VM vm, Vector2 self, object value) => self.x = vm.PyCast(value); + public void x(Vector2 self, object value) => self.x = vm.PyCast(value); [PythonBinding(BindingType.Getter)] - public static object y(VM vm, Vector2 self) => self.y; + public object y(Vector2 self) => self.y; [PythonBinding(BindingType.Setter)] - public static void y(VM vm, Vector2 self, object value) => self.y = vm.PyCast(value); + public void y(Vector2 self, object value) => self.y = vm.PyCast(value); } ``` Once you have done all the above, you must register the type to the VM. -And set the returned object into a module. Here we set it into `builtins` module, so that it can be accessed from anywhere. ```csharp diff --git a/docs/unity/introduction.md b/docs/unity/introduction.md index 26170b68..c4ec1c3d 100644 --- a/docs/unity/introduction.md +++ b/docs/unity/introduction.md @@ -66,6 +66,7 @@ which means passing arguments between C# and Python is extremely easy and intuit | Python Type | C# Type | | ----------- | ------- | | `None` | `PocketPy.NoneType` | +| `object` | `System.Object` | | `bool` | `System.Boolean` | | `int` | `System.Int32` | | `float` | `System.Single` | @@ -75,6 +76,11 @@ which means passing arguments between C# and Python is extremely easy and intuit | `dict` | `System.Collections.Generic.Dictionary` | | ... | ... | +### Python Console in Editor + +PocketPython provides a Python console in Unity editor, +which allows you to do quick debugging and testing. + ### Lua Style API PocketPython also provides a Lua style API for C# in `LuaBindings` class. diff --git a/docs/unity/vm.md b/docs/unity/vm.md index 6d7d5da6..c7fe2fa6 100644 --- a/docs/unity/vm.md +++ b/docs/unity/vm.md @@ -30,6 +30,10 @@ The `VM` class provides a sandboxed Python environment and a set of APIs for int Compile and execute Python source code in the given module. It is equivalent to `Exec(Compile(source, filename, mode), mod)`. ++ `object Eval(string source, PyModule mod = null)` + + Evaluate an expression in the given module. + + `object Call(object callable, object[] args, Dictionary kwargs)` Call a Python callable object with the given arguments and keyword arguments. It is equivalent to `callable(*args, **kwargs)` in Python. @@ -78,6 +82,19 @@ The `VM` class provides a sandboxed Python environment and a set of APIs for int ### Type Conversion ++ `T PyCast(object obj)` + + Convert a Python object to a C# object. It is equivalent to `obj as T` in C#. + Raise `TypeError` if the conversion fails. + ++ `bool IsInstance(object obj, PyTypeObject type)` + + Check if a Python object is an instance of the given type. It is equivalent to `isinstance(obj, type)` in Python. + ++ `void CheckType(object t)` + + Check if `t is T`. Raise `TypeError` if the check fails. + + `bool PyEquals(object lhs, object rhs)` Check if two Python objects are equal. It is equivalent to `lhs == rhs` in Python. This is different from `==` or `object.ReferenceEquals` in C#. You should always use this method to compare Python objects.