// A PHP object, wrapped for access by JavaScript (node/v8).
// Inspired by v8js_object_export in the v8js PHP extension.

// Copyright (c) 2015 C. Scott Ananian <cscott@cscott.net>
#ifndef NODE_PHP_EMBED_NODE_PHP_PHPOBJECT_CLASS_H_
#define NODE_PHP_EMBED_NODE_PHP_PHPOBJECT_CLASS_H_

#include "nan.h"

extern "C" {
#include "main/php.h"
#include "Zend/zend.h"
}

#include "src/values.h" /* for objid_t */

namespace node_php_embed {

class MapperChannel;

class PhpObject : public Nan::ObjectWrap {
 public:
  enum class PropertyOp { GETTER, SETTER, QUERY, DELETER };
  enum class EnumOp { ALL, ONLY_PROPERTY, ONLY_INDEX };
  // Register this class with Node.
  static NAN_MODULE_INIT(Init);
  // Create a new V8 wrapper corresponding to a particular PHP object id.
  static v8::Local<v8::Object> Create(MapperChannel *channel, objid_t id);
  // If the given object is an instance of PhpObject from this channel,
  // set the id field to 0 to indicate an invalid reference to a closed
  // PHP context.
  static void MaybeNeuter(MapperChannel *channel, v8::Local<v8::Object> obj);

 private:
  explicit PhpObject(MapperChannel *channel, objid_t id)
    : channel_(channel), id_(id) { }
  ~PhpObject() override;

  static NAN_METHOD(New);

  // Property access and enumeration
  v8::Local<v8::Array> Enumerate(EnumOp which);
  v8::Local<v8::Value> Property(
      PropertyOp op, v8::Local<v8::String> property,
      v8::Local<v8::Value> new_value = v8::Local<v8::Value>(),
      bool is_index = false);
  // Convenience wrapper to do the index->string conversion.
  v8::Local<v8::Value> Property(
      PropertyOp op, uint32_t index,
      v8::Local<v8::Value> new_value = v8::Local<v8::Value>());

  static NAN_PROPERTY_GETTER(PropertyGet);
  static NAN_PROPERTY_SETTER(PropertySet);
  static NAN_PROPERTY_ENUMERATOR(PropertyEnumerate);
  static NAN_PROPERTY_DELETER(PropertyDelete);
  static NAN_PROPERTY_QUERY(PropertyQuery);
  static NAN_INDEX_GETTER(IndexGet);
  static NAN_INDEX_SETTER(IndexSet);
  static NAN_INDEX_ENUMERATOR(IndexEnumerate);
  static NAN_INDEX_DELETER(IndexDelete);
  static NAN_INDEX_QUERY(IndexQuery);

  // Method invocation
  static void MethodThunk(const Nan::FunctionCallbackInfo<v8::Value>& info);
  void MethodThunk_(v8::Local<v8::String> method,
                   const Nan::FunctionCallbackInfo<v8::Value>& info);

  // PHP-side array access
  static void ArrayOp(PhpObjectMapper *m, PropertyOp op,
                      const ZVal &arr, bool is_array_access,
                      const ZVal &name, const ZVal &value,
                      Value *retval, Value *exception TSRMLS_DC);
  static void ArrayAccessOp(PhpObjectMapper *m, PropertyOp op,
                            const ZVal &arr,
                            const ZVal &name, const ZVal &value,
                            Value *retval, Value *exception TSRMLS_DC);
  static void ArrayEnum(PhpObjectMapper *m, EnumOp op,
                        const ZVal &arr, bool is_array_access,
                        Value *retval, Value *exception TSRMLS_DC);
  static void ArraySize(PhpObjectMapper *m, PropertyOp op, EnumOp which,
                        const ZVal &arr, bool is_array_access,
                        const ZVal &value,
                        Value *retval, Value *exception TSRMLS_DC);

  // Stash away the constructor's template for later use.
  static inline Nan::Persistent<v8::FunctionTemplate> & cons_template() {
    static Nan::Persistent<v8::FunctionTemplate> my_template;
    return my_template;
  }
  static inline v8::Local<v8::Function> constructor() {
    Nan::EscapableHandleScope scope;
    v8::Local<v8::FunctionTemplate> t = Nan::New(cons_template());
    return scope.Escape(Nan::GetFunction(t).ToLocalChecked());
  }
  // Messages (which should have access to PropertyOp)
  class PhpEnumerateMsg;
  class PhpInvokeMsg;
  class PhpPropertyMsg;

  // Members
  MapperChannel *channel_;
  objid_t id_;
};

}  // namespace node_php_embed

#endif  // NODE_PHP_EMBED_NODE_PHP_PHPOBJECT_CLASS_H_
