view runtime/object.c @ 331:61f5b794d939

Breaking change: method call syntax now always uses the syntactic receiver as the actual receiver. This makes its behavior different from function call syntax, but solves some problems with methods being shadowed by local variables and the like.
author Michael Pavone <pavone@retrodev.com>
date Sat, 28 Mar 2015 14:21:04 -0700
parents d2b70cba661e
children
line wrap: on
line source

#include "object.h"
#include <stdarg.h>
#include <stdlib.h>
#include <stddef.h>
#include <gc/gc.h>

void * make_object(obj_meta * meta, void * parent, int num_props, ...)
{
	va_list args;
	object * newobj = GC_MALLOC(meta->size);
	newobj->meta = meta;
	newobj->parent = parent;

	va_start(args, num_props);
	object ** curprop = ((object **)(newobj + 1));
	for (; num_props > 0; num_props--)
	{
		*curprop = va_arg(args, object *);
		curprop++;
	}
	va_end(args);
	return newobj;
}

object * mcall(uint32_t method_id, uint32_t num_args, object * self, ...)
{
	va_list args;
	va_start(args, self);
	object * ret = self->meta->meth_lookup[method_id & 0xF](method_id, num_args, self, args);
	va_end(args);
	return ret;
}

int object_understands(object * obj, uint32_t method_id)
{
	uint32_t slot = method_id & 0xF;
	uint32_t *cur;
	if (!obj->meta->methods[slot]) {
		return 0;
	}
	for (cur = obj->meta->methods[slot]; *cur != LAST_METHOD; cur++) {
		if (*cur == method_id) {
			return 1;
		}
	}
	return 0;
}