1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
#include "stdafx.h"
#include "Future.h"
#include "TemplateList.h"
#include "Engine.h"
#include "Exception.h"
#include "Core/Str.h"
#include "Core/Future.h"
namespace storm {
Type *createFuture(Str *name, ValueArray *params) {
if (params->count() != 1)
return null;
Value param = params->at(0);
if (param.ref)
return null;
return new (params) FutureType(name, param.type);
}
Bool isFuture(Value v) {
return as<FutureType>(v.type) != null;
}
Value unwrapFuture(Value v) {
if (FutureType *t = as<FutureType>(v.type))
return t->param();
else
return v;
}
Value wrapFuture(EnginePtr e, Value v) {
if (v.ref)
return v;
TemplateList *l = e.v.cppTemplate(FutureId);
NameSet *to = l->addTo();
assert(to, L"Too early to use 'wrapFuture'.");
Type *found = as<Type>(to->find(S("Future"), v, Scope()));
if (!found)
throw new (e.v) InternalError(S("Can not find the future type!"));
return Value(found);
}
static void CODECALL futureCreate(void *mem) {
FutureType *t = (FutureType *)runtime::typeOf((RootObject *)mem);
const Handle &h = runtime::typeHandle(t->param().type);
FutureBase *b = new (Place(mem)) FutureBase(h);
runtime::setVTable(b);
}
static void CODECALL futureCreateVoid(void *mem) {
new (Place(mem)) Future<void>();
}
static void CODECALL futureCopy(void *mem, FutureBase *src) {
FutureBase *b = new (Place(mem)) FutureBase(*src);
runtime::setVTable(b);
}
static void CODECALL postVoid(FutureBase *src) {
src->postRaw(null);
}
static void CODECALL resultVoid(FutureBase *src) {
src->resultRaw(null);
}
static void CODECALL postClass(FutureBase *src, Object *post) {
src->postRaw(&post);
}
static RootObject *CODECALL resultClass(FutureBase *src) {
RootObject *result;
src->resultRaw(&result);
return result;
}
FutureType::FutureType(Str *name, Type *contents)
: Type(name, new (name) Array<Value>(1, Value(contents)), typeClass),
contents(contents) {
setSuper(FutureBase::stormType(engine));
}
Value FutureType::param() const {
return Value(contents);
}
Bool FutureType::loadAll() {
Value param = this->param();
// Members differing between implementations.
if (param == Value()) {
loadVoid();
} else if (param.isObject()) {
loadClass();
} else {
loadValue();
}
return Type::loadAll();
}
void FutureType::loadVoid() {
Engine &e = engine;
Value t = thisPtr(this);
//Value cloneEnv = Value(CloneEnv::stormType(e));
add(nativeFunction(e, Value(), Type::CTOR, valList(e, 1, t), address(&futureCreateVoid)));
add(nativeFunction(e, Value(), Type::CTOR, valList(e, 2, t, t), address(&futureCopy)));
add(nativeFunction(e, Value(), S("post"), valList(e, 1, t), address(&postVoid)));
add(nativeFunction(e, Value(), S("result"), valList(e, 1, t), address(&resultVoid)));
}
void FutureType::loadClass() {
Engine &e = engine;
Value t = thisPtr(this);
//Value cloneEnv = Value(CloneEnv::stormType(e));
add(nativeFunction(e, Value(), Type::CTOR, valList(e, 1, t), address(&futureCreate)));
add(nativeFunction(e, Value(), Type::CTOR, valList(e, 2, t, t), address(&futureCopy)));
add(nativeFunction(e, Value(), S("post"), valList(e, 2, t, param()), address(&postClass)));
add(nativeFunction(e, param(), S("result"), valList(e, 1, t), address(&resultClass)));
}
void FutureType::loadValue() {
Engine &e = engine;
Value t = thisPtr(this);
//Value cloneEnv = Value(CloneEnv::stormType(e));
Value ref = param().asRef();
add(nativeFunction(e, Value(), Type::CTOR, valList(e, 1, t), address(&futureCreate)));
add(nativeFunction(e, Value(), Type::CTOR, valList(e, 2, t, t), address(&futureCopy)));
add(nativeFunction(e, Value(), S("post"), valList(e, 2, t, ref), address(&FutureBase::postRaw)));
// TODO: When 'param' is not a built in type, we can use FutureBase::resultRaw() directly,
// as their signatures match (at least on X86).
add(dynamicFunction(e, param(), S("result"), valList(e, 1, t), resultValue()));
}
code::Listing *FutureType::resultValue() {
using namespace code;
Value param = this->param();
Listing *l = new (this) Listing(true, param.desc(engine));
TypeDesc *ptr = engine.ptrDesc();
Var me = l->createParam(ptr);
Var data = l->createVar(l->root(), param.size());
*l << prolog();
*l << lea(ptrA, data);
*l << fnParam(ptr, me);
*l << fnParam(ptr, ptrA);
*l << fnCall(engine.ref(builtin::futureResult), false);
*l << fnRet(data);
return l;
}
}
|