adamc@643: (* Copyright (c) 2009, Adam Chlipala adamc@643: * All rights reserved. adamc@643: * adamc@643: * Redistribution and use in source and binary forms, with or without adamc@643: * modification, are permitted provided that the following conditions are met: adamc@643: * adamc@643: * - Redistributions of source code must retain the above copyright notice, adamc@643: * this list of conditions and the following disclaimer. adamc@643: * - Redistributions in binary form must reproduce the above copyright notice, adamc@643: * this list of conditions and the following disclaimer in the documentation adamc@643: * and/or other materials provided with the distribution. adamc@643: * - The names of contributors may not be used to endorse or promote products adamc@643: * derived from this software without specific prior written permission. adamc@643: * adamc@643: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" adamc@643: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE adamc@643: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE adamc@643: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE adamc@643: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR adamc@643: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF adamc@643: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS adamc@643: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN adamc@643: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) adamc@643: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE adamc@643: * POSSIBILITY OF SUCH DAMAGE. adamc@643: *) adamc@643: adamc@643: structure ScriptCheck :> SCRIPT_CHECK = struct adamc@643: adam@1845: open Mono adamc@643: adamc@643: structure SS = BinarySetFn(struct adamc@643: type ord_key = string adamc@643: val compare = String.compare adamc@643: end) adamc@643: structure IS = IntBinarySet adamc@643: adamc@693: val pushBasis = SS.addList (SS.empty, adamc@693: ["new_channel", adamc@693: "self"]) adamc@799: adamc@643: fun classify (ds, ps) = adamc@643: let adamc@855: val proto = Settings.currentProtocol () adamc@855: adamc@970: fun inString {needle, haystack} = String.isSubstring needle haystack adamc@643: adam@1845: fun hasClient {basis, funcs, push} = adam@1845: MonoUtil.Exp.exists {typ = fn _ => false, adam@1845: exp = fn ERecv _ => push adam@1845: | EFfiApp ("Basis", x, _) => SS.member (basis, x) adam@1845: | EJavaScript _ => not push adam@1845: | ENamed n => IS.member (funcs, n) adam@1845: | _ => false} adamc@643: adamc@693: fun decl ((d, _), (pull_ids, push_ids)) = adamc@643: let adam@1845: val hasClientPull = hasClient {basis = SS.empty, funcs = pull_ids, push = false} adam@1845: val hasClientPush = hasClient {basis = pushBasis, funcs = push_ids, push = true} adamc@643: in adamc@643: case d of adam@1845: DVal (_, n, _, e, _) => (if hasClientPull e then adamc@693: IS.add (pull_ids, n) adamc@693: else adamc@693: pull_ids, adamc@693: if hasClientPush e then adamc@693: IS.add (push_ids, n) adamc@693: else adamc@693: push_ids) adam@1845: | DValRec xes => (if List.exists (fn (_, _, _, e, _) => hasClientPull e) xes then adamc@693: foldl (fn ((_, n, _, _, _), pull_ids) => IS.add (pull_ids, n)) adamc@693: pull_ids xes adamc@693: else adamc@693: pull_ids, adam@1845: if List.exists (fn (_, _, _, e, _) => hasClientPush e) xes then adamc@693: foldl (fn ((_, n, _, _, _), push_ids) => IS.add (push_ids, n)) adamc@693: push_ids xes adamc@693: else adamc@693: push_ids) adamc@693: | _ => (pull_ids, push_ids) adamc@643: end adamc@643: adamc@693: val (pull_ids, push_ids) = foldl decl (IS.empty, IS.empty) ds adamc@643: adamc@855: val foundBad = ref false adamc@855: adam@1845: val all_ids = IS.union (pull_ids, push_ids) adam@1845: adam@1845: val ps = map (fn n => adam@1845: (n, if IS.member (push_ids, n) then adam@1845: (if not (#persistent proto) andalso not (!foundBad) then adam@1845: (foundBad := true; adam@1845: ErrorMsg.error ("This program needs server push, but the current protocol (" adam@1845: ^ #name proto ^ ") doesn't support that.")) adam@1845: else adam@1845: (); adam@1845: ServerAndPullAndPush) adam@1845: else if IS.member (pull_ids, n) then adam@1845: ServerAndPull adam@1845: else adam@2056: ServerOnly, AnyDb)) (IS.listItems all_ids) adamc@643: in adamc@643: (ds, ps) adamc@643: end adamc@643: adamc@643: end adamc@643: