mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Enable ES6 classes.
R=abarth@chromium.org Review URL: https://codereview.chromium.org/742183002
This commit is contained in:
parent
e599443dc2
commit
4e68e8f732
@ -191,16 +191,6 @@ static void timerTraceProfilerInMainThread(const char* name, int status)
|
||||
}
|
||||
}
|
||||
|
||||
static void initializeV8Common(v8::Isolate* isolate)
|
||||
{
|
||||
v8::V8::AddGCPrologueCallback(V8GCController::gcPrologue);
|
||||
v8::V8::AddGCEpilogueCallback(V8GCController::gcEpilogue);
|
||||
|
||||
v8::Debug::SetLiveEditEnabled(isolate, false);
|
||||
|
||||
isolate->SetAutorunMicrotasks(false);
|
||||
}
|
||||
|
||||
void V8Initializer::initializeMainThreadIfNeeded()
|
||||
{
|
||||
ASSERT(isMainThread());
|
||||
@ -210,11 +200,19 @@ void V8Initializer::initializeMainThreadIfNeeded()
|
||||
return;
|
||||
initialized = true;
|
||||
|
||||
static const char v8Flags[] = "--harmony-classes";
|
||||
v8::V8::SetFlagsFromString(v8Flags, sizeof(v8Flags) - 1);
|
||||
|
||||
gin::IsolateHolder::Initialize(gin::IsolateHolder::kStrictMode, v8ArrayBufferAllocator());
|
||||
|
||||
v8::Isolate* isolate = V8PerIsolateData::initialize();
|
||||
|
||||
initializeV8Common(isolate);
|
||||
v8::V8::AddGCPrologueCallback(V8GCController::gcPrologue);
|
||||
v8::V8::AddGCEpilogueCallback(V8GCController::gcEpilogue);
|
||||
|
||||
v8::Debug::SetLiveEditEnabled(isolate, false);
|
||||
|
||||
isolate->SetAutorunMicrotasks(false);
|
||||
|
||||
v8::V8::SetFatalErrorHandler(reportFatalErrorInMainThread);
|
||||
v8::V8::AddMessageListener(messageHandlerInMainThread);
|
||||
|
||||
14
tests/js/classes-expected.txt
Normal file
14
tests/js/classes-expected.txt
Normal file
@ -0,0 +1,14 @@
|
||||
Running 10 tests
|
||||
ok 1 ES6 classes should create instances
|
||||
ok 2 ES6 classes should create subclasses
|
||||
ok 3 ES6 classes should create anonymous classes
|
||||
ok 4 ES6 classes should put methods on the prototype
|
||||
ok 5 ES6 classes should call methods with |this|
|
||||
ok 6 ES6 classes should let toMethod rebind super
|
||||
ok 7 ES6 classes should support super() constructor calls
|
||||
ok 8 ES6 classes should automatically call super() in default constructor
|
||||
ok 9 ES6 classes should call super.method()
|
||||
ok 10 ES6 classes should support getters and setters
|
||||
10 tests
|
||||
10 pass
|
||||
0 fail
|
||||
163
tests/js/classes.sky
Normal file
163
tests/js/classes.sky
Normal file
@ -0,0 +1,163 @@
|
||||
<sky>
|
||||
<import src="../resources/chai.sky" />
|
||||
<import src="../resources/mocha.sky" />
|
||||
<script>
|
||||
describe("ES6 classes", function() {
|
||||
it("should create instances", function() {
|
||||
class Example {}
|
||||
var instance = new Example();
|
||||
assert.instanceOf(instance, Example);
|
||||
});
|
||||
|
||||
it("should create subclasses", function() {
|
||||
class Parent {}
|
||||
class Child extends Parent {}
|
||||
var instance = new Child();
|
||||
assert.instanceOf(instance, Child);
|
||||
assert.instanceOf(instance, Parent);
|
||||
});
|
||||
|
||||
it("should create anonymous classes", function() {
|
||||
var Parent = class {}
|
||||
var Child = class extends Parent {}
|
||||
var instance = new Child();
|
||||
assert.instanceOf(instance, Child);
|
||||
assert.instanceOf(instance, Parent);
|
||||
});
|
||||
|
||||
it("should put methods on the prototype", function() {
|
||||
class Test {
|
||||
exampleMethod() { }
|
||||
}
|
||||
var instance = new Test();
|
||||
assert.isFalse(instance.hasOwnProperty("exampleMethod"));
|
||||
var proto = Object.getPrototypeOf(instance);
|
||||
assert.isTrue(proto.hasOwnProperty("exampleMethod"));
|
||||
});
|
||||
|
||||
it("should call methods with |this|", function() {
|
||||
class Adder {
|
||||
constructor(value) {
|
||||
this.value = value;
|
||||
}
|
||||
add(other) {
|
||||
return this.value + other;
|
||||
}
|
||||
}
|
||||
|
||||
var adder = new Adder(10);
|
||||
assert.equal(adder.add(15), 25);
|
||||
});
|
||||
|
||||
it("should let toMethod rebind super", function() {
|
||||
var getValue = function() {
|
||||
assert.isFunction(this.value);
|
||||
assert.isFunction(this.superValue);
|
||||
return this.value() + super.superValue();
|
||||
}
|
||||
|
||||
class HolderParent {
|
||||
superValue() {
|
||||
return 5;
|
||||
}
|
||||
}
|
||||
|
||||
class Holder extends HolderParent {
|
||||
constructor() {
|
||||
this.value_ = 5;
|
||||
}
|
||||
value() {
|
||||
return this.value_;
|
||||
}
|
||||
}
|
||||
|
||||
var holder = new Holder();
|
||||
var getValueMethod = getValue.toMethod(Holder.prototype);
|
||||
assert.equal(getValueMethod.call(holder), 10);
|
||||
});
|
||||
|
||||
it("should support super() constructor calls", function() {
|
||||
class Parent {
|
||||
constructor(value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
|
||||
class Child extends Parent {
|
||||
constructor(value) {
|
||||
super(value + 5);
|
||||
}
|
||||
}
|
||||
|
||||
var child = new Child(10);
|
||||
assert.equal(child.value, 15);
|
||||
});
|
||||
|
||||
it("should automatically call super() in default constructor", function() {
|
||||
class Parent {
|
||||
constructor() {
|
||||
this.value = 10;
|
||||
}
|
||||
}
|
||||
|
||||
class Child extends Parent {
|
||||
}
|
||||
|
||||
var child = new Child();
|
||||
assert.equal(child.value, 10);
|
||||
});
|
||||
|
||||
it("should call super.method()", function() {
|
||||
class Parent {
|
||||
value() {
|
||||
return 10;
|
||||
}
|
||||
}
|
||||
|
||||
class Child extends Parent {
|
||||
value() {
|
||||
return super.value() + 5;
|
||||
}
|
||||
}
|
||||
|
||||
var child = new Child();
|
||||
assert.equal(child.value(), 15);
|
||||
});
|
||||
|
||||
it("should support getters and setters", function() {
|
||||
class Variable {
|
||||
constructor(value) {
|
||||
this.value_ = value;
|
||||
}
|
||||
|
||||
get value() {
|
||||
return this.value_;
|
||||
}
|
||||
|
||||
set value(newValue) {
|
||||
this.value_ = newValue;
|
||||
}
|
||||
}
|
||||
|
||||
var variable = new Variable("first");
|
||||
|
||||
// Methods are on the prototype.
|
||||
var proto = Object.getPrototypeOf(variable);
|
||||
var descriptor = Object.getOwnPropertyDescriptor(proto, "value");
|
||||
assert.isObject(descriptor);
|
||||
assert.isFunction(descriptor.get);
|
||||
assert.isFunction(descriptor.set);
|
||||
assert.isUndefined(descriptor.value);
|
||||
|
||||
// Getter and setter should not be on the instance.
|
||||
assert.isUndefined(Object.getOwnPropertyDescriptor(variable, "value"));
|
||||
|
||||
assert.equal(variable.value, "first");
|
||||
assert.equal(variable.value_, "first");
|
||||
variable.value = "second";
|
||||
assert.equal(variable.value, "second");
|
||||
assert.equal(variable.value_, "second");
|
||||
});
|
||||
});
|
||||
</script>
|
||||
</sky>
|
||||
Loading…
x
Reference in New Issue
Block a user