forked from google/traceur-compiler
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract class runtime functions into own file
See google#1007
- Loading branch information
Showing
4 changed files
with
217 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// Copyright 2014 Traceur Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
(function() { | ||
'use strict'; | ||
|
||
var $Object = Object; | ||
var $TypeError = TypeError; | ||
var $create = $Object.create; | ||
var $defineProperties = $traceurRuntime.defineProperties; | ||
var $defineProperty = $traceurRuntime.defineProperty; | ||
var $getOwnPropertyDescriptor = $traceurRuntime.getOwnPropertyDescriptor; | ||
var $getOwnPropertyNames = $traceurRuntime.getOwnPropertyNames; | ||
var $getPrototypeOf = Object.getPrototypeOf; | ||
|
||
function superDescriptor(homeObject, name) { | ||
var proto = $getPrototypeOf(homeObject); | ||
do { | ||
var result = $getOwnPropertyDescriptor(proto, name); | ||
if (result) | ||
return result; | ||
proto = $getPrototypeOf(proto); | ||
} while (proto) | ||
|
||
return undefined; | ||
} | ||
|
||
function superCall(self, homeObject, name, args) { | ||
return superGet(self, homeObject, name).apply(self, args); | ||
} | ||
|
||
function superGet(self, homeObject, name) { | ||
var descriptor = superDescriptor(homeObject, name); | ||
if (descriptor) { | ||
if (!descriptor.get) | ||
return descriptor.value; | ||
return descriptor.get.call(self); | ||
} | ||
return undefined; | ||
} | ||
|
||
function superSet(self, homeObject, name, value) { | ||
var descriptor = superDescriptor(homeObject, name); | ||
if (descriptor && descriptor.set) { | ||
descriptor.set.call(self, value); | ||
return value; | ||
} | ||
throw $TypeError("super has no setter '" + name + "'."); | ||
} | ||
|
||
function getDescriptors(object) { | ||
var descriptors = {}, name, names = $getOwnPropertyNames(object); | ||
for (var i = 0; i < names.length; i++) { | ||
var name = names[i]; | ||
descriptors[name] = $getOwnPropertyDescriptor(object, name); | ||
} | ||
return descriptors; | ||
} | ||
|
||
// The next three functions are more or less identical to | ||
// ClassDefinitionEvaluation in the ES6 draft. | ||
|
||
function createClass(ctor, object, staticObject, superClass) { | ||
$defineProperty(object, 'constructor', { | ||
value: ctor, | ||
configurable: true, | ||
enumerable: false, | ||
writable: true | ||
}); | ||
|
||
if (arguments.length > 3) { | ||
if (typeof superClass === 'function') | ||
ctor.__proto__ = superClass; | ||
ctor.prototype = $create(getProtoParent(superClass), | ||
getDescriptors(object)); | ||
} else { | ||
ctor.prototype = object; | ||
} | ||
$defineProperty(ctor, 'prototype', {configurable: false, writable: false}); | ||
return $defineProperties(ctor, getDescriptors(staticObject)); | ||
} | ||
|
||
function getProtoParent(superClass) { | ||
if (typeof superClass === 'function') { | ||
var prototype = superClass.prototype; | ||
if ($Object(prototype) === prototype || prototype === null) | ||
return superClass.prototype; | ||
} | ||
if (superClass === null) | ||
return null; | ||
throw new $TypeError(); | ||
} | ||
|
||
function defaultSuperCall(self, homeObject, args) { | ||
if ($getPrototypeOf(homeObject) !== null) | ||
superCall(self, homeObject, 'constructor', args); | ||
} | ||
|
||
$traceurRuntime.createClass = createClass; | ||
$traceurRuntime.defaultSuperCall = defaultSuperCall; | ||
$traceurRuntime.superCall = superCall; | ||
$traceurRuntime.superGet = superGet; | ||
$traceurRuntime.superSet = superSet; | ||
})(); |
Oops, something went wrong.