Bug 1185106 - Part 1: ShellPromise boilerplate. (r=efaust)

This commit is contained in:
Till Schneidereit 2015-10-05 13:24:03 -07:00
Родитель 7b3ec1ac59
Коммит cdde94a546
8 изменённых файлов: 167 добавлений и 0 удалений

Просмотреть файл

@ -294,6 +294,7 @@ selfhosting_srcs := \
$(srcdir)/builtin/Module.js \
$(srcdir)/builtin/Number.js \
$(srcdir)/builtin/Object.js \
$(srcdir)/builtin/Promise.js \
$(srcdir)/builtin/Reflect.js \
$(srcdir)/builtin/RegExp.js \
$(srcdir)/builtin/String.js \

Просмотреть файл

@ -0,0 +1,95 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* vim: set ts=8 sts=4 et sw=4 tw=99:
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "builtin/Promise.h"
#include "jscntxt.h"
using namespace js;
static const JSFunctionSpec promise_methods[] = {
JS_SELF_HOSTED_FN("catch", "Promise_catch", 1, 0),
JS_SELF_HOSTED_FN("then", "Promise_then", 2, 0),
JS_FS_END
};
static const JSFunctionSpec promise_static_methods[] = {
JS_SELF_HOSTED_FN("all", "Promise_all", 1, 0),
JS_SELF_HOSTED_FN("race", "Promise_race", 1, 0),
JS_SELF_HOSTED_FN("reject", "Promise_reject", 1, 0),
JS_SELF_HOSTED_FN("resolve", "Promise_resolve", 1, 0),
JS_FS_END
};
static bool
PromiseConstructor(JSContext* cx, unsigned argc, Value* vp)
{
CallArgs args = CallArgsFromVp(argc, vp);
JSObject* obj = NewBuiltinClassInstance(cx, &ShellPromiseObject::class_);
if (!obj)
return false;
// TODO: store the resolve and reject callbacks.
args.rval().setObject(*obj);
return true;
}
static JSObject*
CreatePromisePrototype(JSContext* cx, JSProtoKey key)
{
return cx->global()->createBlankPrototype(cx, &ShellPromiseObject::protoClass_);
}
const Class ShellPromiseObject::class_ = {
"ShellPromise",
JSCLASS_HAS_CACHED_PROTO(JSProto_ShellPromise),
nullptr, /* addProperty */
nullptr, /* delProperty */
nullptr, /* getProperty */
nullptr, /* setProperty */
nullptr, /* enumerate */
nullptr, /* resolve */
nullptr, /* mayResolve */
nullptr, /* finalize */
nullptr, /* call */
nullptr, /* hasInstance */
nullptr, /* construct */
nullptr, /* trace */
{
GenericCreateConstructor<PromiseConstructor, 2, gc::AllocKind::FUNCTION>,
CreatePromisePrototype,
promise_static_methods,
nullptr,
promise_methods
}
};
const Class ShellPromiseObject::protoClass_ = {
"ShellPromise",
JSCLASS_HAS_CACHED_PROTO(JSProto_ShellPromise),
nullptr, /* addProperty */
nullptr, /* delProperty */
nullptr, /* getProperty */
nullptr, /* setProperty */
nullptr, /* enumerate */
nullptr, /* resolve */
nullptr, /* mayResolve */
nullptr, /* finalize */
nullptr, /* call */
nullptr, /* hasInstance */
nullptr, /* construct */
nullptr, /* trace */
{
DELEGATED_CLASSSPEC(&ShellPromiseObject::class_.spec),
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
ClassSpec::IsDelegated
}
};

25
js/src/builtin/Promise.h Normal file
Просмотреть файл

@ -0,0 +1,25 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* vim: set ts=8 sts=4 et sw=4 tw=99:
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef builtin_Promise_h
#define builtin_Promise_h
#include "vm/NativeObject.h"
namespace js {
class AutoSetNewObjectMetadata;
class ShellPromiseObject : public NativeObject
{
public:
static const Class class_;
static const Class protoClass_;
};
} // namespace js
#endif /* builtin_Promise_h */

27
js/src/builtin/Promise.js Normal file
Просмотреть файл

@ -0,0 +1,27 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
function Promise_all(iterable) {
global.print("Hi, I'm Promise_all. Please implement me.");
}
function Promise_race(iterable) {
global.print("Hi, I'm Promise_race. Please implement me.");
}
function Promise_reject(iterable) {
global.print("Hi, I'm Promise_reject. Please implement me.");
}
function Promise_resolve(iterable) {
global.print("Hi, I'm Promise_resolve. Please implement me.");
}
function Promise_catch(onRejected) {
global.print("Hi, I'm Promise_catch. Please implement me.");
}
function Promise_then(onFulfilled, onRejected) {
global.print("Hi, I'm Promise_then. Please implement me.");
}

Просмотреть файл

@ -56,6 +56,12 @@
#define IF_SAB(real,imaginary) imaginary
#endif
#ifdef NIGHTLY_BUILD
#define IF_NIGHTLY(real,imaginary) real
#else
#define IF_NIGHTLY(real,imaginary) imaginary
#endif
#define JS_FOR_PROTOTYPES(real,imaginary) \
imaginary(Null, 0, InitNullClass, dummy) \
real(Object, 1, InitViaClassSpec, OCLASP(Plain)) \
@ -115,6 +121,7 @@ IF_SAB(real,imaginary)(Atomics, 53, InitAtomicsClass, OCLASP
real(Module, 55, InitModuleClass, OCLASP(Module)) \
real(ImportEntry, 56, InitImportEntryClass, OCLASP(ImportEntry)) \
real(ExportEntry, 57, InitExportEntryClass, OCLASP(ExportEntry)) \
IF_NIGHTLY(real, imaginary)(ShellPromise, 58, InitViaClassSpec, OCLASP(ShellPromise)) \
#define JS_FOR_EACH_PROTOTYPE(macro) JS_FOR_PROTOTYPES(macro,macro)

Просмотреть файл

@ -152,6 +152,7 @@ UNIFIED_SOURCES += [
'builtin/ModuleObject.cpp',
'builtin/Object.cpp',
'builtin/Profilers.cpp',
'builtin/Promise.cpp',
'builtin/Reflect.cpp',
'builtin/ReflectParse.cpp',
'builtin/SIMD.cpp',

Просмотреть файл

@ -23,6 +23,11 @@
#include "builtin/MapObject.h"
#include "builtin/ModuleObject.h"
#include "builtin/Object.h"
#ifdef NIGHTLY_BUILD
#include "builtin/Promise.h"
#endif
#include "builtin/RegExp.h"
#include "builtin/SIMD.h"
#include "builtin/SymbolObject.h"

Просмотреть файл

@ -382,6 +382,12 @@ class GlobalObject : public NativeObject
return &global->getPrototype(JSProto_Symbol).toObject().as<NativeObject>();
}
static NativeObject* getOrCreatePromisePrototype(JSContext* cx, Handle<GlobalObject*> global) {
if (!ensureConstructor(cx, global, JSProto_ShellPromise))
return nullptr;
return &global->getPrototype(JSProto_ShellPromise).toObject().as<NativeObject>();
}
static NativeObject* getOrCreateRegExpPrototype(JSContext* cx, Handle<GlobalObject*> global) {
if (!ensureConstructor(cx, global, JSProto_RegExp))
return nullptr;