Skip to content

Commit

Permalink
Add ability to bind typed arrays to script API
Browse files Browse the repository at this point in the history
Note: Only replaced 2 instances to test, Node.get_children and TileMap.get_used_cells
Note: Will do a mass replace on later PRs of whathever I can find, but probably need
a tool to grep through doc.
Warning: Mono will break, needs to be fixed (and so do TypeScript and NativeScript, need to ask respective maintainers)
  • Loading branch information
reduz committed Apr 21, 2020
1 parent 7343ec1 commit 5d4dc2d
Show file tree
Hide file tree
Showing 24 changed files with 452 additions and 10 deletions.
65 changes: 63 additions & 2 deletions core/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,19 @@

#include "array.h"

#include "container_type_validate.h"
#include "core/hashfuncs.h"
#include "core/object.h"
#include "core/script_language.h"
#include "core/variant.h"
#include "core/vector.h"

class ArrayPrivate {
public:
SafeRefCount refcount;
Vector<Variant> array;

ContainerTypeValidate typed;
};

void Array::_ref(const Array &p_from) const {
Expand Down Expand Up @@ -108,12 +112,34 @@ uint32_t Array::hash() const {
}
return h;
}
void Array::operator=(const Array &p_array) {

_ref(p_array);
void Array::_assign(const Array &p_array) {

if (_p->typed.type != Variant::OBJECT && _p->typed.type == p_array._p->typed.type) {
//same type or untyped, just reference, shuold be fine
_ref(p_array);
} else if (_p->typed.type == Variant::NIL) { //from typed to untyped, must copy, but this is cheap anyway
_p->array = p_array._p->array;
} else if (p_array._p->typed.type == Variant::NIL) { //from untyped to typed, must try to check if they are all valid
for (int i = 0; i < p_array._p->array.size(); i++) {
if (!_p->typed.validate(p_array._p->array[i], "assign")) {
return;
}
}
_p->array = p_array._p->array; //then just copy, which is cheap anyway
} else if (_p->typed.can_reference(p_array._p->typed)) { //same type or compatible
_ref(p_array);
} else {
ERR_FAIL_MSG("Assignment of arrays of incompatible types.");
}
}

void Array::operator=(const Array &p_array) {
_assign(p_array);
}
void Array::push_back(const Variant &p_value) {

ERR_FAIL_COND(!_p->typed.validate(p_value, "push_back"));
_p->array.push_back(p_value);
}

Expand All @@ -124,11 +150,13 @@ Error Array::resize(int p_new_size) {

void Array::insert(int p_pos, const Variant &p_value) {

ERR_FAIL_COND(!_p->typed.validate(p_value, "insert"));
_p->array.insert(p_pos, p_value);
}

void Array::erase(const Variant &p_value) {

ERR_FAIL_COND(!_p->typed.validate(p_value, "erase"));
_p->array.erase(p_value);
}

Expand All @@ -144,13 +172,15 @@ Variant Array::back() const {

int Array::find(const Variant &p_value, int p_from) const {

ERR_FAIL_COND_V(!_p->typed.validate(p_value, "find"), -1);
return _p->array.find(p_value, p_from);
}

int Array::rfind(const Variant &p_value, int p_from) const {

if (_p->array.size() == 0)
return -1;
ERR_FAIL_COND_V(!_p->typed.validate(p_value, "rfind"), -1);

if (p_from < 0) {
// Relative offset from the end
Expand All @@ -173,11 +203,13 @@ int Array::rfind(const Variant &p_value, int p_from) const {

int Array::find_last(const Variant &p_value) const {

ERR_FAIL_COND_V(!_p->typed.validate(p_value, "find_last"), -1);
return rfind(p_value);
}

int Array::count(const Variant &p_value) const {

ERR_FAIL_COND_V(!_p->typed.validate(p_value, "count"), 0);
if (_p->array.size() == 0)
return 0;

Expand All @@ -193,6 +225,8 @@ int Array::count(const Variant &p_value) const {
}

bool Array::has(const Variant &p_value) const {
ERR_FAIL_COND_V(!_p->typed.validate(p_value, "use 'has'"), false);

return _p->array.find(p_value, 0) != -1;
}

Expand All @@ -203,6 +237,8 @@ void Array::remove(int p_pos) {

void Array::set(int p_idx, const Variant &p_value) {

ERR_FAIL_COND(!_p->typed.validate(p_value, "set"));

operator[](p_idx) = p_value;
}

Expand All @@ -216,6 +252,7 @@ Array Array::duplicate(bool p_deep) const {
Array new_arr;
int element_count = size();
new_arr.resize(element_count);
new_arr._p->typed = _p->typed;
for (int i = 0; i < element_count; i++) {
new_arr[i] = p_deep ? get(i).duplicate(p_deep) : get(i);
}
Expand Down Expand Up @@ -369,11 +406,13 @@ _FORCE_INLINE_ int bisect(const Vector<Variant> &p_array, const Variant &p_value

int Array::bsearch(const Variant &p_value, bool p_before) {

ERR_FAIL_COND_V(!_p->typed.validate(p_value, "binary search"), -1);
return bisect(_p->array, p_value, p_before, _ArrayVariantSort());
}

int Array::bsearch_custom(const Variant &p_value, Object *p_obj, const StringName &p_function, bool p_before) {

ERR_FAIL_COND_V(!_p->typed.validate(p_value, "custom binary search"), -1);
ERR_FAIL_NULL_V(p_obj, 0);

_ArrayVariantSortCustom less;
Expand All @@ -391,6 +430,7 @@ Array &Array::invert() {

void Array::push_front(const Variant &p_value) {

ERR_FAIL_COND(!_p->typed.validate(p_value, "push_front"));
_p->array.insert(0, p_value);
}

Expand Down Expand Up @@ -465,6 +505,27 @@ const void *Array::id() const {
return _p->array.ptr();
}

Array::Array(const Array &p_from, uint32_t p_type, const StringName &p_class_name, const Variant &p_script) {
_p = memnew(ArrayPrivate);
_p->refcount.init();
set_typed(p_type, p_class_name, p_script);
_assign(p_from);
}

void Array::set_typed(uint32_t p_type, const StringName &p_class_name, const Variant &p_script) {
ERR_FAIL_COND_MSG(_p->array.size() > 0, "Type can only be set when array is empty.");
ERR_FAIL_COND_MSG(_p->refcount.get() > 1, "Type can only be set when array has no more than one user.");
ERR_FAIL_COND_MSG(_p->typed.type != Variant::NIL, "Type can only be set once.");
ERR_FAIL_COND_MSG(p_class_name != StringName() && p_type != Variant::OBJECT, "Class names can only be set for type OBJECT");
Ref<Script> script = p_script;
ERR_FAIL_COND_MSG(script.is_valid() && p_class_name == StringName(), "Script class can only be set together with base class name");

_p->typed.type = Variant::Type(p_type);
_p->typed.class_name = p_class_name;
_p->typed.script = script;
_p->typed.where = "TypedArray";
}

Array::Array(const Array &p_from) {

_p = nullptr;
Expand Down
5 changes: 5 additions & 0 deletions core/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ class Array {
int _clamp_index(int p_index) const;
static int _fix_slice_index(int p_index, int p_arr_len, int p_top_mod);

protected:
Array(const Array &p_base, uint32_t p_type, const StringName &p_class_name, const Variant &p_script);
void _assign(const Array &p_array);

public:
Variant &operator[](int p_idx);
const Variant &operator[](int p_idx) const;
Expand Down Expand Up @@ -101,6 +105,7 @@ class Array {

const void *id() const;

void set_typed(uint32_t p_type, const StringName &p_class_name, const Variant &p_script);
Array(const Array &p_from);
Array();
~Array();
Expand Down
98 changes: 98 additions & 0 deletions core/container_type_validate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#ifndef CONTAINER_TYPE_VALIDATE_H
#define CONTAINER_TYPE_VALIDATE_H

#include "core/script_language.h"
#include "core/variant.h"

struct ContainerTypeValidate {

Variant::Type type = Variant::NIL;
StringName class_name;
Ref<Script> script;
const char *where = "conatiner";

_FORCE_INLINE_ bool can_reference(const ContainerTypeValidate &p_type) const {
if (type == p_type.type) {
if (type != Variant::OBJECT) {
return true; //nothing else to check
}
} else {
return false;
}

//both are object

if ((class_name != StringName()) != (p_type.class_name != StringName())) {
return false; //both need to have class or none
}

if (class_name != p_type.class_name) {
if (!ClassDB::is_parent_class(p_type.class_name, class_name)) {
return false;
}
}

if (script.is_null() != p_type.script.is_null()) {
return false;
}

if (script != p_type.script) {
if (!p_type.script->inherits_script(script)) {
return false;
}
}

return true;
}

_FORCE_INLINE_ bool validate(const Variant &p_variant, const char *p_operation = "use") {

if (type == Variant::NIL) {
return true;
}

ERR_FAIL_COND_V_MSG(type != p_variant.get_type(), false, "Attempted to " + String(p_operation) + " a variable of type '" + Variant::get_type_name(p_variant.get_type()) + "' into a " + where + " of type '" + Variant::get_type_name(type) + "'.");
if (type != p_variant.get_type()) {
return false;
}

if (type != Variant::OBJECT) {
return true;
}
#ifdef DEBUG_ENABLED
ObjectID object_id = p_variant;
if (object_id == ObjectID()) {
return true; //fine its null;
}
Object *object = ObjectDB::get_instance(object_id);
ERR_FAIL_COND_V_MSG(object == nullptr, false, "Attempted to " + String(p_operation) + " an invalid (previously freed?) object instance into a '" + String(where) + ".");
#else
Object *object = p_variant;
if (object == nullptr) {
return true; //fine
}
#endif
if (class_name == StringName()) {
return true; //all good, no class type requested
}

StringName obj_class = object->get_class_name();
if (obj_class != class_name) {
ERR_FAIL_COND_V_MSG(!ClassDB::is_parent_class(object->get_class_name(), class_name), false, "Attempted to " + String(p_operation) + " an object of type '" + object->get_class() + "' into a " + where + ", which does not inherit from '" + String(class_name) + "'.");
}

if (script.is_null()) {
return true; //all good
}

Ref<Script> other_script = object->get_script();

//check base script..
ERR_FAIL_COND_V_MSG(other_script.is_null(), false, "Attempted to " + String(p_operation) + " an object into a " + String(where) + ", that does not inherit from '" + String(script->get_class_name()) + "'.");
ERR_FAIL_COND_V_MSG(!other_script->inherits_script(script), false, "Attempted to " + String(p_operation) + " an object into a " + String(where) + ", that does not inherit from '" + String(script->get_class_name()) + "'.");

return true;
}
};

#endif // CONTAINER_TYPE_VALIDATE_H
1 change: 1 addition & 0 deletions core/method_bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "core/method_ptrcall.h"
#include "core/object.h"
#include "core/type_info.h"
#include "core/typedefs.h"
#include "core/variant.h"

#include <stdio.h>
Expand Down
1 change: 1 addition & 0 deletions core/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ enum PropertyHint {
PROPERTY_HINT_NODE_PATH_VALID_TYPES,
PROPERTY_HINT_SAVE_FILE, ///< a file path must be passed, hint_text (optionally) is a filter "*.png,*.wav,*.doc,". This opens a save dialog
PROPERTY_HINT_INT_IS_OBJECTID,
PROPERTY_HINT_ARRAY_TYPE,
PROPERTY_HINT_MAX,
// When updating PropertyHint, also sync the hardcoded list in VisualScriptEditorVariableEdit
};
Expand Down
2 changes: 2 additions & 0 deletions core/script_language.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ class Script : public Resource {

virtual Ref<Script> get_base_script() const = 0; //for script inheritance

virtual bool inherits_script(const Ref<Script> &p_script) const = 0;

virtual StringName get_instance_base_type() const = 0; // this may not work in all scripts, will return empty if so
virtual ScriptInstance *instance_create(Object *p_this) = 0;
virtual PlaceHolderScriptInstance *placeholder_instance_create(Object *p_this) { return nullptr; }
Expand Down
1 change: 1 addition & 0 deletions core/typed_array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "typed_array.h"
Loading

0 comments on commit 5d4dc2d

Please sign in to comment.