Auto merge of #14096 - fflorent:11485-make-dom-methods-taking-mut-JSContent-unsafe, r=nox

11485 make dom methods taking mut js content unsafe

This is a rebased version of PR #11595

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes fix #11485

<!-- Either: -->
- [ ] There are tests for these changes OR
- [x] These changes do not require tests because: no code logic was changed

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->

---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/14096)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2016-11-17 02:44:54 -06:00 committed by GitHub
commit f70fc6644d
28 changed files with 274 additions and 226 deletions

View file

@ -2316,10 +2316,13 @@ class CGAbstractMethod(CGThing):
arguments.
docs is None or documentation for the method in a string.
unsafe is used to add the decorator 'unsafe' to a function, giving as a result
an 'unsafe fn()' declaration.
"""
def __init__(self, descriptor, name, returnType, args, inline=False,
alwaysInline=False, extern=False, unsafe_fn=False, pub=False,
templateArgs=None, unsafe=False, docs=None, doesNotPanic=False):
alwaysInline=False, extern=False, unsafe=False, pub=False,
templateArgs=None, docs=None, doesNotPanic=False):
CGThing.__init__(self)
self.descriptor = descriptor
self.name = name
@ -2327,10 +2330,9 @@ class CGAbstractMethod(CGThing):
self.args = args
self.alwaysInline = alwaysInline
self.extern = extern
self.unsafe_fn = extern or unsafe_fn
self.unsafe = extern or unsafe
self.templateArgs = templateArgs
self.pub = pub
self.unsafe = unsafe
self.docs = docs
self.catchPanic = self.extern and not doesNotPanic
@ -2357,7 +2359,7 @@ class CGAbstractMethod(CGThing):
if self.pub:
decorators.append('pub')
if self.unsafe_fn:
if self.unsafe:
decorators.append('unsafe')
if self.extern:
@ -2373,10 +2375,6 @@ class CGAbstractMethod(CGThing):
def define(self):
body = self.definition_body()
# Method will already be marked `unsafe` if `self.extern == True`
if self.unsafe and not self.extern:
body = CGWrapper(CGIndenter(body), pre="unsafe {\n", post="\n}")
if self.catchPanic:
body = CGWrapper(CGIndenter(body),
pre="return wrap_panic(|| {\n",
@ -2409,7 +2407,7 @@ class CGConstructorEnabled(CGAbstractMethod):
'ConstructorEnabled', 'bool',
[Argument("*mut JSContext", "aCx"),
Argument("HandleObject", "aObj")],
unsafe_fn=True)
unsafe=True)
def definition_body(self):
conditions = []
@ -3089,7 +3087,7 @@ class CGDefineDOMInterfaceMethod(CGAbstractMethod):
Argument('HandleObject', 'global'),
]
CGAbstractMethod.__init__(self, descriptor, 'DefineDOMInterface',
'void', args, pub=True, unsafe_fn=True)
'void', args, pub=True, unsafe=True)
def define(self):
return CGAbstractMethod.define(self)
@ -5349,10 +5347,19 @@ class CGInterfaceTrait(CGThing):
def fmt(arguments):
return "".join(", %s: %s" % argument for argument in arguments)
methods = [
CGGeneric("fn %s(&self%s) -> %s;\n" % (name, fmt(arguments), rettype))
for name, arguments, rettype in members()
]
def contains_unsafe_arg(arguments):
if not arguments or len(arguments) == 0:
return False
return reduce((lambda x, y: x or y[1] == '*mut JSContext'), arguments, False)
methods = []
for name, arguments, rettype in members():
arguments = list(arguments)
methods.append(CGGeneric("%sfn %s(&self%s) -> %s;\n" % (
'unsafe ' if contains_unsafe_arg(arguments) else '',
name, fmt(arguments), rettype))
)
if methods:
self.cgRoot = CGWrapper(CGIndenter(CGList(methods, "")),
pre="pub trait %sMethods {\n" % descriptor.interface.identifier.name,

View file

@ -85,7 +85,7 @@ impl<T: Reflectable + JSTraceable + Iterable> IterableIterator<T> {
/// Create a new iterator instance for the provided iterable DOM interface.
pub fn new(iterable: &T,
type_: IteratorType,
wrap: fn(*mut JSContext, &GlobalScope, Box<IterableIterator<T>>)
wrap: unsafe fn(*mut JSContext, &GlobalScope, Box<IterableIterator<T>>)
-> Root<Self>) -> Root<Self> {
let iterator = box IterableIterator {
reflector: Reflector::new(),

View file

@ -16,12 +16,14 @@ use std::ptr;
pub fn reflect_dom_object<T, U>(
obj: Box<T>,
global: &U,
wrap_fn: fn(*mut JSContext, &GlobalScope, Box<T>) -> Root<T>)
wrap_fn: unsafe fn(*mut JSContext, &GlobalScope, Box<T>) -> Root<T>)
-> Root<T>
where T: Reflectable, U: DerivedFrom<GlobalScope>
{
let global_scope = global.upcast();
wrap_fn(global_scope.get_cx(), global_scope, obj)
unsafe {
wrap_fn(global_scope.get_cx(), global_scope, obj)
}
}
/// A struct to store a reference to the reflector of a DOM object.